|
1 | | -import { createRouter, createRoute, createRootRoute, Outlet } from '@tanstack/react-router'; |
| 1 | +import { createRouter, createRoute, createRootRoute, Outlet, redirect } from '@tanstack/react-router'; |
2 | 2 |
|
3 | 3 | // Pages |
4 | 4 | import { LandingPage } from '@/pages/landing'; |
@@ -31,12 +31,28 @@ const authRoute = createRoute({ |
31 | 31 | const signInRoute = createRoute({ |
32 | 32 | getParentRoute: () => authRoute, |
33 | 33 | 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 | + }, |
34 | 42 | component: SignInPage, |
35 | 43 | }); |
36 | 44 |
|
37 | 45 | const signUpRoute = createRoute({ |
38 | 46 | getParentRoute: () => authRoute, |
39 | 47 | 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 | + }, |
40 | 56 | component: SignUpPage, |
41 | 57 | }); |
42 | 58 |
|
@@ -80,10 +96,18 @@ const publicInvoiceRoute = createRoute({ |
80 | 96 | }), |
81 | 97 | }); |
82 | 98 |
|
83 | | -// Protected routes wrapper |
| 99 | +// Protected routes wrapper - redirects to signin if not authenticated |
84 | 100 | const protectedRoute = createRoute({ |
85 | 101 | getParentRoute: () => rootRoute, |
86 | 102 | 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 | + }, |
87 | 111 | component: () => <Outlet />, |
88 | 112 | }); |
89 | 113 |
|
|
0 commit comments