Skip to content

Commit 387b76d

Browse files
authored
Merge pull request #61 from hypersign-protocol/implement/refreshToken
implemented refresh token based api call
2 parents e12e7e6 + a2f9e24 commit 387b76d

File tree

6 files changed

+264
-245
lines changed

6 files changed

+264
-245
lines changed

src/App.vue

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,8 @@ export default {
362362
const userDetails = localStorage.getItem("user");
363363
if (userDetails) {
364364
try {
365-
this.userDetails = JSON.parse(userDetails);
365+
const parsed = JSON.parse(userDetails);
366+
Object.assign(this.userDetails, parsed);
366367
this.user = this.userDetails;
367368
this.loggedInUserEmailId = this.user?.accessAccount?.email;
368369
this.setIsLoggedOut(true);
@@ -483,7 +484,8 @@ export default {
483484
try {
484485
const userDetails = localStorage.getItem("user");
485486
if (userDetails) {
486-
this.userDetails = JSON.parse(userDetails);
487+
const parsed = JSON.parse(userDetails);
488+
Object.assign(this.userDetails, parsed);
487489
this.parseAuthToken= this.userDetails
488490
this.setIsLoggedOut(true)
489491
const redirectPath=localStorage.getItem("postLoginRedirect")||'/studio/dashboard'

src/router.js

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -209,12 +209,10 @@ router.beforeEach(async (to, from, next) => {
209209
next(false)
210210
return router.push('/404')
211211
}
212+
document.title = to.meta.title;
212213
if (to.matched.some(record => record.meta.requiresAuth)) {
213-
document.title = to.meta.title;
214-
const url = `${config.studioServer.BASE_URL}api/v1/auth`
215214
try {
216-
217-
const response = await fetch(url, {
215+
const response = await fetch(`${config.studioServer.BASE_URL}api/v1/auth`, {
218216
method: "POST",
219217
credentials: "include",
220218
});
@@ -225,20 +223,34 @@ router.beforeEach(async (to, from, next) => {
225223
localStorage.setItem("user", JSON.stringify(json.message));
226224
store.commit('playgroundStore/addUserDetailsToProfile', json.message)
227225
next()
228-
} else {
229-
throw new Error("Unexpected response");
230226
}
231227
} 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+
}
239252
}
240253
} else {
241-
console.log(to.path)
242254
if (to.path === '/studio/login') {
243255
store.commit('mainStore/setIsLoggedOut', false)
244256
}

0 commit comments

Comments
 (0)