mirror of
https://github.com/Floriansylvain/RenewCMS.git
synced 2026-08-19 19:53:21 +02:00
Added cookies managment for Auth purpose
This commit is contained in:
@@ -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 }
|
||||
})
|
||||
Reference in New Issue
Block a user