TODO: check token expired
This commit is contained in:
parent
22dc204fdf
commit
c167b22414
@ -2,11 +2,12 @@ import {defineStore} from 'pinia'
|
|||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
import {useConfigStore} from "./config";
|
import {useConfigStore} from "./config";
|
||||||
|
|
||||||
export const useUserStore = defineStore('user', {
|
const useUserStore = defineStore('user', {
|
||||||
persist: true,
|
persist: true,
|
||||||
state: () => ({
|
state: () => ({
|
||||||
refresh_token: null as null | string,
|
refresh_token: null as null | string,
|
||||||
jwt_token: null as null | string,
|
jwt_token: null as null | string,
|
||||||
|
expired_at: null as null | Date,
|
||||||
}),
|
}),
|
||||||
getters: {
|
getters: {
|
||||||
isLoggedIn(): boolean {
|
isLoggedIn(): boolean {
|
||||||
@ -18,6 +19,10 @@ export const useUserStore = defineStore('user', {
|
|||||||
this.refresh_token = refresh_token
|
this.refresh_token = refresh_token
|
||||||
this.jwt_token = jwt_token
|
this.jwt_token = jwt_token
|
||||||
},
|
},
|
||||||
|
setExpired(expires_in: number) {
|
||||||
|
// get date(current + seconds)
|
||||||
|
this.expired_at = new Date(Date.now() + expires_in * 1000)
|
||||||
|
},
|
||||||
logout() {
|
logout() {
|
||||||
console.log("logout")
|
console.log("logout")
|
||||||
this.refresh_token = null
|
this.refresh_token = null
|
||||||
@ -40,3 +45,19 @@ export const useUserStore = defineStore('user', {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
setInterval(() => {
|
||||||
|
// check expired
|
||||||
|
const currentDate = new Date()
|
||||||
|
|
||||||
|
const userStore = useUserStore()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
})
|
||||||
|
|
||||||
|
export {
|
||||||
|
useUserStore
|
||||||
|
}
|
@ -93,6 +93,9 @@ const validate = () => {
|
|||||||
if (res.data.refresh_token && res.data.token) {
|
if (res.data.refresh_token && res.data.token) {
|
||||||
userStore.login(res.data.refresh_token, res.data.token);
|
userStore.login(res.data.refresh_token, res.data.token);
|
||||||
|
|
||||||
|
userStore.setExpired(res.data.expires_in)
|
||||||
|
console.log(userStore.expired_at)
|
||||||
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
window.location.href = "/";
|
window.location.href = "/";
|
||||||
}, 100);
|
}, 100);
|
||||||
|
Loading…
Reference in New Issue
Block a user