Feature/standardize ui display
This commit is contained in:
Vendored
+72
-1
@@ -14,7 +14,78 @@ const methods = {
|
||||
* @return {Boolean} isLogin
|
||||
*/
|
||||
isLogin: function () {
|
||||
return localStorage['Access Token'] ? true : false;
|
||||
if (!localStorage['Access Token']) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (this.isSessionExpired()) {
|
||||
this.clearSession();
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
},
|
||||
|
||||
isSessionExpired: function () {
|
||||
var expiresAt = localStorage.getItem('token_expires_at');
|
||||
if (!expiresAt) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return Date.now() >= new Date(expiresAt).getTime();
|
||||
},
|
||||
|
||||
clearSession: function () {
|
||||
localStorage.removeItem('Access Token');
|
||||
localStorage.removeItem('token_expires_at');
|
||||
localStorage.removeItem('admin_role');
|
||||
localStorage.removeItem('admin_session');
|
||||
localStorage.removeItem('is_impersonating');
|
||||
delete axios.defaults.headers.common['Authorization'];
|
||||
},
|
||||
|
||||
persistLoginSession: function (token, expiresAt, options) {
|
||||
options = options || {};
|
||||
var bearer = token.indexOf('Bearer ') === 0 ? token : 'Bearer ' + token;
|
||||
localStorage['Access Token'] = bearer;
|
||||
|
||||
if (expiresAt) {
|
||||
localStorage.setItem('token_expires_at', expiresAt);
|
||||
} else {
|
||||
localStorage.removeItem('token_expires_at');
|
||||
}
|
||||
|
||||
if (options.adminRole !== undefined && options.adminRole !== null) {
|
||||
localStorage.setItem('admin_role', String(options.adminRole));
|
||||
}
|
||||
|
||||
if (options.adminSession) {
|
||||
localStorage.setItem('admin_session', '1');
|
||||
}
|
||||
|
||||
if (options.clearAdminSession) {
|
||||
localStorage.removeItem('admin_role');
|
||||
localStorage.removeItem('admin_session');
|
||||
}
|
||||
|
||||
this.setAuthorization();
|
||||
},
|
||||
|
||||
handleUnauthorized: function (router) {
|
||||
var isAdminPath = window.location.pathname.indexOf('/admin') === 0;
|
||||
this.clearSession();
|
||||
|
||||
if (!router || !router.currentRoute) {
|
||||
window.location.href = isAdminPath ? '/admin/login' : '/login';
|
||||
return;
|
||||
}
|
||||
|
||||
var routeName = router.currentRoute.name;
|
||||
if (routeName === 'Voter Login' || routeName === 'Admin Login') {
|
||||
return;
|
||||
}
|
||||
|
||||
router.replace({ name: isAdminPath ? 'Admin Login' : 'Voter Login' });
|
||||
},
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user