@@ -209,12 +209,10 @@ router.beforeEach(async (to, from, next) => {
209
209
next ( false )
210
210
return router . push ( '/404' )
211
211
}
212
+ document . title = to . meta . title ;
212
213
if ( to . matched . some ( record => record . meta . requiresAuth ) ) {
213
- document . title = to . meta . title ;
214
- const url = `${ config . studioServer . BASE_URL } api/v1/auth`
215
214
try {
216
-
217
- const response = await fetch ( url , {
215
+ const response = await fetch ( `${ config . studioServer . BASE_URL } api/v1/auth` , {
218
216
method : "POST" ,
219
217
credentials : "include" ,
220
218
} ) ;
@@ -225,20 +223,34 @@ router.beforeEach(async (to, from, next) => {
225
223
localStorage . setItem ( "user" , JSON . stringify ( json . message ) ) ;
226
224
store . commit ( 'playgroundStore/addUserDetailsToProfile' , json . message )
227
225
next ( )
228
- } else {
229
- throw new Error ( "Unexpected response" ) ;
230
226
}
231
227
} catch ( e ) {
232
- console . log ( e )
233
- store . commit ( 'mainStore/setMainSideNavBar' , false )
234
- next ( {
235
- path : '/studio/login' ,
236
- query : { redirect : to . fullPath }
237
- } )
238
-
228
+ try {
229
+ const refreshResponse = await fetch ( `${ config . studioServer . BASE_URL } api/v1/auth/refresh` , {
230
+ method : "POST" ,
231
+ credentials : "include" ,
232
+ } ) ;
233
+ if ( ! refreshResponse . ok ) {
234
+ throw new Error ( 'Refresh failed' ) ;
235
+ }
236
+ const authResponse = await fetch ( `${ config . studioServer . BASE_URL } api/v1/auth` , {
237
+ method : "POST" ,
238
+ credentials : "include" ,
239
+ } ) ;
240
+ if ( authResponse . ok ) {
241
+ const user = await authResponse . json ( ) ;
242
+ localStorage . setItem ( "user" , JSON . stringify ( user . message ) ) ;
243
+ store . commit ( 'playgroundStore/addUserDetailsToProfile' , user . message ) ;
244
+ next ( ) ;
245
+ } else {
246
+ throw new Error ( 'Refresh token invalid' ) ;
247
+ }
248
+ } catch ( refreshError ) {
249
+ store . commit ( 'mainStore/setMainSideNavBar' , false ) ;
250
+ next ( { path : '/studio/login' , query : { redirect : to . fullPath } } ) ;
251
+ }
239
252
}
240
253
} else {
241
- console . log ( to . path )
242
254
if ( to . path === '/studio/login' ) {
243
255
store . commit ( 'mainStore/setIsLoggedOut' , false )
244
256
}
0 commit comments