diff --git a/src/stores/login.js b/src/stores/login.js index 0e9fc6c..674c635 100644 --- a/src/stores/login.js +++ b/src/stores/login.js @@ -1,6 +1,7 @@ import { defineStore } from "pinia"; import axios from 'axios'; import apiError from '@/module/apiError.js'; +import { deleteCookie, setCookie } from "../utils/cookieUtil"; export default defineStore('loginStore', { // data, methods, computed @@ -40,6 +41,7 @@ export default defineStore('loginStore', { // document.cookie = `luciaRefreshToken=${refreshToken};expires=${new Date(this.expired)};`; this.isLoggedIn = true; + setCookie("isLuciaLoggedIn", "true"); // 大部分的情況下,預設導向至 FILES 頁面 // 然而有一種情況是使用者在沒有登入的情況下貼上了某一個頁面的網址, @@ -85,6 +87,7 @@ export default defineStore('loginStore', { document.cookie = 'luciaToken=; expires=Thu, 01 Jan 1970 00:00:00 UTC;'; this.isLoggedIn = false; + deleteCookie("isLuciaLoggedIn"); this.$router.push('/login'); }, diff --git a/src/utils/cookieUtil.js b/src/utils/cookieUtil.js new file mode 100644 index 0000000..c13ca71 --- /dev/null +++ b/src/utils/cookieUtil.js @@ -0,0 +1,29 @@ +export function getCookie(name) { + const nameEQ = name + "="; + const ca = document.cookie.split(';'); + for (let i = 0; i < ca.length; i++) { + let c = ca[i]; + while (c.charAt(0) === ' ') { + c = c.substring(1, c.length); + } + if (c.indexOf(nameEQ) === 0) { + return c.substring(nameEQ.length, c.length); + } + } + return null; + } + + export function setCookie(name, value, days=1) { + let expires = ""; + if (days) { + const date = new Date(); + date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)); + expires = "; expires=" + date.toUTCString(); + } + document.cookie = name + "=" + (value || "") + expires + "; path=/"; + } + + export function deleteCookie(name, path = '/') { + document.cookie = name + '=; Max-Age=-99999999; path=' + path; + } + \ No newline at end of file diff --git a/src/views/MainContainer.vue b/src/views/MainContainer.vue index 198fa2b..8165ff8 100644 --- a/src/views/MainContainer.vue +++ b/src/views/MainContainer.vue @@ -20,6 +20,7 @@ import Loading from '@/components/Loading.vue'; import { leaveFilter, leaveConformance } from '@/module/alertModal.js'; import PageAdminStore from '@/stores/pageAdmin.js'; import LoginStore from "@/stores/login.js"; +import { getCookie } from "../utils/cookieUtil.js"; export default { name: 'MainContainer', @@ -67,7 +68,7 @@ export default { beforeRouteEnter(to, from, next) { // 重新整理畫面以及第一次進入網頁時,beforeRouteEnter這個hook會被執行,然而beforeRouteUpdate不會被執行 const loginStore = LoginStore(); - if (!loginStore.isLoggedIn) { + if (!getCookie("isLuciaLoggedIn")) { next({ path: '/login', query: {