forked from verdnatura/hedera-web
Renew token logic
This commit is contained in:
parent
261fddb1cf
commit
0246e39f0f
|
@ -29,21 +29,28 @@ export const useUserStore = defineStore('user', {
|
|||
},
|
||||
|
||||
getters: {
|
||||
loggedIn: state => state.token != null,
|
||||
loggedIn: state => !!state.token,
|
||||
userLocaleOption: state =>
|
||||
state.localeOptions?.find(l => l.value === state?.user?.lang)
|
||||
state.localeOptions?.find(l => l.value === state?.user?.lang),
|
||||
storage: state => (state.keepLogin ? localStorage : sessionStorage)
|
||||
},
|
||||
|
||||
actions: {
|
||||
async init() {
|
||||
this.isGuest = localStorage.getItem('hederaGuest') || false;
|
||||
const autoLoginStatus = await this.tryAutoLogin();
|
||||
if (!autoLoginStatus) {
|
||||
this.router.push({ name: 'login' });
|
||||
await this.getToken();
|
||||
if (!this.loggedIn) {
|
||||
const autoLoginStatus = await this.tryAutoLogin();
|
||||
if (!autoLoginStatus) {
|
||||
this.router.push({ name: 'login' });
|
||||
}
|
||||
return;
|
||||
}
|
||||
await this.fetchTokenConfig();
|
||||
await this.fetchUser();
|
||||
await this.supplantInit();
|
||||
this.updateSiteLocale();
|
||||
this.startInterval();
|
||||
},
|
||||
|
||||
getToken() {
|
||||
|
@ -55,8 +62,7 @@ export const useUserStore = defineStore('user', {
|
|||
},
|
||||
|
||||
setToken(token) {
|
||||
const storage = this.keepLogin ? localStorage : sessionStorage;
|
||||
storage.setItem('vnToken', token);
|
||||
this.storage.setItem('vnToken', token);
|
||||
this.token = token;
|
||||
},
|
||||
|
||||
|
@ -79,7 +85,6 @@ export const useUserStore = defineStore('user', {
|
|||
tokenMultimedia: 'Accounts/logout',
|
||||
token: 'VnUsers/logout'
|
||||
};
|
||||
const storage = this.keepLogin ? localStorage : sessionStorage;
|
||||
let destroyTokenPromises = [];
|
||||
try {
|
||||
if (destroyTokens) {
|
||||
|
@ -88,7 +93,8 @@ export const useUserStore = defineStore('user', {
|
|||
);
|
||||
if (isValidToken)
|
||||
destroyTokenPromises = Object.entries(tokens).map(
|
||||
([key, url]) => this.destroyToken(url, storage, key)
|
||||
([key, url]) =>
|
||||
this.destroyToken(url, this.storage, key)
|
||||
);
|
||||
}
|
||||
} finally {
|
||||
|
@ -100,12 +106,15 @@ export const useUserStore = defineStore('user', {
|
|||
}
|
||||
},
|
||||
|
||||
isLoggedIn() {
|
||||
const token = this.getToken();
|
||||
return !!token;
|
||||
},
|
||||
|
||||
setSession(data) {
|
||||
const storage = this.keepLogin ? localStorage : sessionStorage;
|
||||
storage.setItem('vnToken', data.token);
|
||||
storage.setItem('created', data.created);
|
||||
storage.setItem('ttl', data.ttl);
|
||||
sessionStorage.setItem('keepLogin', this.keepLogin);
|
||||
this.storage.setItem('created', data.created);
|
||||
this.storage.setItem('ttl', data.ttl);
|
||||
localStorage.setItem('keepLogin', this.keepLogin);
|
||||
},
|
||||
|
||||
async login(user, password, remember) {
|
||||
|
@ -135,15 +144,11 @@ export const useUserStore = defineStore('user', {
|
|||
},
|
||||
|
||||
async logout() {
|
||||
if (this.token != null) {
|
||||
try {
|
||||
await api.post('Accounts/logout');
|
||||
} catch (e) {}
|
||||
localStorage.removeItem('vnToken');
|
||||
sessionStorage.removeItem('vnToken');
|
||||
}
|
||||
try {
|
||||
await api.post('Accounts/logout');
|
||||
} catch (e) {}
|
||||
this.destroy();
|
||||
this.$reset();
|
||||
localStorage.removeItem('hederaGuest');
|
||||
useAppStore().onLogout();
|
||||
},
|
||||
|
||||
|
@ -154,8 +159,7 @@ export const useUserStore = defineStore('user', {
|
|||
});
|
||||
if (!data) return;
|
||||
this.tokenConfig = data;
|
||||
sessionStorage.setItem('tokenConfig', data);
|
||||
sessionStorage.setItem('renewPeriod', data.renewPeriod);
|
||||
this.storage.setItem('renewPeriod', data.renewPeriod);
|
||||
return data;
|
||||
} catch (error) {
|
||||
notify('errors.tokenConfig', 'negative');
|
||||
|
@ -173,17 +177,14 @@ export const useUserStore = defineStore('user', {
|
|||
},
|
||||
|
||||
async checkValidity() {
|
||||
const tokenConfig =
|
||||
this.tokenConfig ?? sessionStorage.getItem('tokenConfig');
|
||||
const storage = this.keepLogin ? localStorage : sessionStorage;
|
||||
const created = +this.storage.getItem('created');
|
||||
const ttl = +this.storage.getItem('ttl');
|
||||
|
||||
const created = +storage.getItem('created');
|
||||
const ttl = +storage.getItem('ttl');
|
||||
if (this.isCheckingToken || !created) return;
|
||||
this.isCheckingToken = true;
|
||||
|
||||
const renewPeriodInSeconds =
|
||||
Math.min(ttl, tokenConfig.value.renewPeriod) * 1000;
|
||||
Math.min(ttl, this.tokenConfig?.renewPeriod) * 1000;
|
||||
const maxDate = created + renewPeriodInSeconds;
|
||||
const now = new Date().getTime();
|
||||
|
||||
|
@ -201,7 +202,7 @@ export const useUserStore = defineStore('user', {
|
|||
|
||||
startInterval() {
|
||||
this.stopRenewer();
|
||||
const renewPeriod = +sessionStorage.getItem('renewPeriod');
|
||||
const renewPeriod = +this.storage.getItem('renewPeriod');
|
||||
if (!renewPeriod) return;
|
||||
this.intervalId = setInterval(
|
||||
() => this.checkValidity(),
|
||||
|
|
Loading…
Reference in New Issue