diff --git a/frontend/src/modules/auth/auth-service.js b/frontend/src/modules/auth/auth-service.js index ad32d53c0a..ab413deeb3 100644 --- a/frontend/src/modules/auth/auth-service.js +++ b/frontend/src/modules/auth/auth-service.js @@ -72,7 +72,16 @@ export class AuthService { } static fetchMe() { - return authAxios.get('/auth/me').then((response) => response.data); + return authAxios.get('/auth/me').then((response) => { + const { data } = response; + localStorage.setItem('user', JSON.stringify(data)); + localStorage.setItem('userDateTime', `${new Date().getTime()}`); + return data; + }); + } + + static fetchMeLocally() { + return JSON.parse(localStorage.getItem('user')); } static signout() { diff --git a/frontend/src/modules/auth/store/actions.js b/frontend/src/modules/auth/store/actions.js index f4916b7049..0138c7aafe 100644 --- a/frontend/src/modules/auth/store/actions.js +++ b/frontend/src/modules/auth/store/actions.js @@ -19,8 +19,22 @@ export default { try { const token = AuthToken.get(); if (token) { - const currentUser = await AuthService.fetchMe(); + const userDate = localStorage.getItem('userDateTime'); + if (userDate) { + const dateDiff = new Date().getTime() - +userDate; + if (dateDiff > (7 * 24 * 60 * 60 * 1000)) { + localStorage.removeItem('user'); + localStorage.removeItem('userDateTime'); + } + } + const currentUserLocally = AuthService.fetchMeLocally(); connectSocket(token); + if (currentUserLocally) { + commit('AUTH_INIT_SUCCESS', { currentUser: currentUserLocally }); + + return currentUserLocally; + } + const currentUser = await AuthService.fetchMe(); commit('AUTH_INIT_SUCCESS', { currentUser }); return currentUser; } @@ -31,6 +45,7 @@ export default { } catch (error) { console.error(error); disconnectSocket(); + console.log(error); commit('AUTH_INIT_ERROR'); dispatch('doSignout'); return null; @@ -149,6 +164,7 @@ export default { commit('AUTH_SUCCESS', { currentUser: null, }); + localStorage.removeItem('user'); router.push('/auth/signin'); },