Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion frontend/src/modules/auth/auth-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
18 changes: 17 additions & 1 deletion frontend/src/modules/auth/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -31,6 +45,7 @@ export default {
} catch (error) {
console.error(error);
disconnectSocket();
console.log(error);
commit('AUTH_INIT_ERROR');
dispatch('doSignout');
return null;
Expand Down Expand Up @@ -149,6 +164,7 @@ export default {
commit('AUTH_SUCCESS', {
currentUser: null,
});
localStorage.removeItem('user');
router.push('/auth/signin');
},

Expand Down