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 {useConfigStore} from "./config";
|
||||
|
||||
export const useUserStore = defineStore('user', {
|
||||
const useUserStore = defineStore('user', {
|
||||
persist: true,
|
||||
state: () => ({
|
||||
refresh_token: null as null | string,
|
||||
jwt_token: null as null | string,
|
||||
expired_at: null as null | Date,
|
||||
}),
|
||||
getters: {
|
||||
isLoggedIn(): boolean {
|
||||
@ -18,6 +19,10 @@ export const useUserStore = defineStore('user', {
|
||||
this.refresh_token = refresh_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() {
|
||||
console.log("logout")
|
||||
this.refresh_token = null
|
||||
@ -39,4 +44,20 @@ export const useUserStore = defineStore('user', {
|
||||
return this.jwt_token
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
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) {
|
||||
userStore.login(res.data.refresh_token, res.data.token);
|
||||
|
||||
userStore.setExpired(res.data.expires_in)
|
||||
console.log(userStore.expired_at)
|
||||
|
||||
setTimeout(() => {
|
||||
window.location.href = "/";
|
||||
}, 100);
|
||||
|
Loading…
Reference in New Issue
Block a user