Skip to content

Commit 2eceaeb

Browse files
committed
fix: add proper auth guards to router
- Protected routes now redirect to /auth/signin if not authenticated - Sign in/sign up pages redirect to /dashboard if already authenticated - Uses TanStack Router's beforeLoad hook with redirect
1 parent 4c09d54 commit 2eceaeb

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

apps/web/src/router.tsx

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createRouter, createRoute, createRootRoute, Outlet } from '@tanstack/react-router';
1+
import { createRouter, createRoute, createRootRoute, Outlet, redirect } from '@tanstack/react-router';
22

33
// Pages
44
import { LandingPage } from '@/pages/landing';
@@ -31,12 +31,28 @@ const authRoute = createRoute({
3131
const signInRoute = createRoute({
3232
getParentRoute: () => authRoute,
3333
path: 'signin',
34+
beforeLoad: ({ context }) => {
35+
const { auth } = context as { auth: { isAuthenticated: boolean } };
36+
if (auth.isAuthenticated) {
37+
throw redirect({
38+
to: '/dashboard',
39+
});
40+
}
41+
},
3442
component: SignInPage,
3543
});
3644

3745
const signUpRoute = createRoute({
3846
getParentRoute: () => authRoute,
3947
path: 'signup',
48+
beforeLoad: ({ context }) => {
49+
const { auth } = context as { auth: { isAuthenticated: boolean } };
50+
if (auth.isAuthenticated) {
51+
throw redirect({
52+
to: '/dashboard',
53+
});
54+
}
55+
},
4056
component: SignUpPage,
4157
});
4258

@@ -80,10 +96,18 @@ const publicInvoiceRoute = createRoute({
8096
}),
8197
});
8298

83-
// Protected routes wrapper
99+
// Protected routes wrapper - redirects to signin if not authenticated
84100
const protectedRoute = createRoute({
85101
getParentRoute: () => rootRoute,
86102
id: 'protected',
103+
beforeLoad: ({ context }) => {
104+
const { auth } = context as { auth: { isAuthenticated: boolean } };
105+
if (!auth.isAuthenticated) {
106+
throw redirect({
107+
to: '/auth/signin',
108+
});
109+
}
110+
},
87111
component: () => <Outlet />,
88112
});
89113

0 commit comments

Comments
 (0)