@@ -42,10 +42,17 @@ const rootRoute = createRootRoute({
4242 component : Layout ,
4343} )
4444
45- async function requireAuth ( ) {
45+ function buildReturnTo ( location : { pathname : string ; searchStr ?: string ; hash ?: string } ) {
46+ return `${ location . pathname } ${ location . searchStr ?? '' } ${ location . hash ?? '' } `
47+ }
48+
49+ async function requireAuth ( { location } : { location : { pathname : string ; searchStr ?: string ; hash ?: string } } ) {
4650 const user = await getCurrentUser ( )
4751 if ( ! user ) {
48- throw redirect ( { to : '/login' } )
52+ throw redirect ( {
53+ to : '/login' ,
54+ search : { returnTo : buildReturnTo ( location ) } ,
55+ } )
4956 }
5057 return { user }
5158}
@@ -59,12 +66,18 @@ const skillsRoute = createRoute({
5966const loginRoute = createRoute ( {
6067 getParentRoute : ( ) => rootRoute ,
6168 path : 'login' ,
69+ validateSearch : ( search : Record < string , unknown > ) => ( {
70+ returnTo : typeof search . returnTo === 'string' ? search . returnTo : '' ,
71+ } ) ,
6272 component : LoginPage ,
6373} )
6474
6575const registerRoute = createRoute ( {
6676 getParentRoute : ( ) => rootRoute ,
6777 path : 'register' ,
78+ validateSearch : ( search : Record < string , unknown > ) => ( {
79+ returnTo : typeof search . returnTo === 'string' ? search . returnTo : '' ,
80+ } ) ,
6881 component : RegisterPage ,
6982} )
7083
@@ -152,8 +165,8 @@ const dashboardReviewDetailRoute = createRoute({
152165const dashboardPromotionsRoute = createRoute ( {
153166 getParentRoute : ( ) => rootRoute ,
154167 path : 'dashboard/promotions' ,
155- beforeLoad : async ( ) => {
156- const { user } = await requireAuth ( )
168+ beforeLoad : async ( ctx ) => {
169+ const { user } = await requireAuth ( ctx )
157170 if ( ! user . platformRoles ?. includes ( 'SKILL_ADMIN' ) && ! user . platformRoles ?. includes ( 'SUPER_ADMIN' ) ) {
158171 throw redirect ( { to : '/dashboard' } )
159172 }
@@ -199,8 +212,8 @@ const settingsAccountsRoute = createRoute({
199212const adminUsersRoute = createRoute ( {
200213 getParentRoute : ( ) => rootRoute ,
201214 path : 'admin/users' ,
202- beforeLoad : async ( ) => {
203- const { user } = await requireAuth ( )
215+ beforeLoad : async ( ctx ) => {
216+ const { user } = await requireAuth ( ctx )
204217 if ( ! user . platformRoles ?. includes ( 'USER_ADMIN' ) && ! user . platformRoles ?. includes ( 'SUPER_ADMIN' ) ) {
205218 throw redirect ( { to : '/dashboard' } )
206219 }
@@ -212,8 +225,8 @@ const adminUsersRoute = createRoute({
212225const adminAuditLogRoute = createRoute ( {
213226 getParentRoute : ( ) => rootRoute ,
214227 path : 'admin/audit-log' ,
215- beforeLoad : async ( ) => {
216- const { user } = await requireAuth ( )
228+ beforeLoad : async ( ctx ) => {
229+ const { user } = await requireAuth ( ctx )
217230 if ( ! user . platformRoles ?. includes ( 'AUDITOR' ) && ! user . platformRoles ?. includes ( 'SUPER_ADMIN' ) ) {
218231 throw redirect ( { to : '/dashboard' } )
219232 }
0 commit comments