Added cookies managment for Auth purpose

This commit is contained in:
Florian Sylvain
2022-12-23 20:35:55 +01:00
parent eee7c1e067
commit 96c3f596ae
6 changed files with 171 additions and 40 deletions
+35 -3
View File
@@ -1,5 +1,7 @@
import { getCookie } from "@/utils/cookies";
import { defineStore } from "pinia";
import { ref } from "vue";
import { useErrorsStore } from "./ErrorsStore";
export interface jwtFormat {
code: number,
@@ -8,8 +10,38 @@ export interface jwtFormat {
}
export const useAuthStore = defineStore("AuthStore", () => {
const expire = ref("")
const token = ref("")
const expire = ref("")
const token = ref("")
function isSet(): boolean {
return token.value !== undefined && token.value !== ""
}
function isExpired(): boolean {
const tokenDate = new Date(expire.value)
const currentDate = new Date()
return { expire, token }
if (currentDate.getTime() > tokenDate.getTime()) {
useErrorsStore().sessionExpired = true
return true
}
return false
}
function isValid(): boolean {
return isSet() && !isExpired()
}
function initStore(): void {
const JWTtoken = getCookie('JWTtoken')
const JWTexpire = getCookie('JWTexpire')
if (JWTtoken !== "" && JWTexpire !== "") {
token.value = JWTtoken
expire.value = JWTexpire
}
}
initStore()
return { expire, token, isValid }
})