Claptrap-ui/src/stores/user.ts

42 lines
990 B
TypeScript
Raw Normal View History

2022-05-21 01:07:31 +02:00
import { defineStore } from "pinia";
2022-05-22 16:25:13 +02:00
import * as jose from "jose";
2022-05-24 23:04:35 +02:00
import { cookiesStorage } from "./coockiesStorage";
import type { RouteLocationRaw } from "vue-router";
2022-05-21 01:07:31 +02:00
2022-05-22 16:25:13 +02:00
export const useUserStore = defineStore("user", {
2022-05-21 01:07:31 +02:00
state: () => ({
2022-05-22 16:25:13 +02:00
userName: "",
discordId: "",
2022-05-24 23:04:35 +02:00
discriminator: "",
avatar: "",
2022-05-22 16:25:13 +02:00
token: "",
loginFail: false,
2022-05-28 16:25:45 +02:00
asExpired: false,
originRoute: undefined as RouteLocationRaw | undefined,
2022-05-21 01:07:31 +02:00
}),
getters: {
2022-05-22 16:25:13 +02:00
isLoggedIn(): boolean {
if (this.token) {
return !this.isExpired;
}
return false;
},
isExpired(): boolean {
if (this.getTokenPayload?.exp) {
const exp = new Date(this.getTokenPayload.exp * 1000);
2022-05-28 16:25:45 +02:00
const expired = exp < new Date();
this.asExpired = expired;
return expired;
2022-05-22 16:25:13 +02:00
}
return true;
},
getTokenPayload: (state) => {
return jose.decodeJwt(state.token);
},
},
2022-05-27 20:13:41 +02:00
actions: {},
2022-05-24 23:04:35 +02:00
persist: {
storage: cookiesStorage,
},
2022-05-21 01:07:31 +02:00
});