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