fix #216 with cookie mgmt.
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
import { defineStore } from "pinia";
|
import { defineStore } from "pinia";
|
||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
import apiError from '@/module/apiError.js';
|
import apiError from '@/module/apiError.js';
|
||||||
|
import { deleteCookie, setCookie } from "../utils/cookieUtil";
|
||||||
|
|
||||||
export default defineStore('loginStore', {
|
export default defineStore('loginStore', {
|
||||||
// data, methods, computed
|
// data, methods, computed
|
||||||
@@ -40,6 +41,7 @@ export default defineStore('loginStore', {
|
|||||||
// document.cookie = `luciaRefreshToken=${refreshToken};expires=${new Date(this.expired)};`;
|
// document.cookie = `luciaRefreshToken=${refreshToken};expires=${new Date(this.expired)};`;
|
||||||
|
|
||||||
this.isLoggedIn = true;
|
this.isLoggedIn = true;
|
||||||
|
setCookie("isLuciaLoggedIn", "true");
|
||||||
|
|
||||||
// 大部分的情況下,預設導向至 FILES 頁面
|
// 大部分的情況下,預設導向至 FILES 頁面
|
||||||
// 然而有一種情況是使用者在沒有登入的情況下貼上了某一個頁面的網址,
|
// 然而有一種情況是使用者在沒有登入的情況下貼上了某一個頁面的網址,
|
||||||
@@ -85,6 +87,7 @@ export default defineStore('loginStore', {
|
|||||||
document.cookie = 'luciaToken=; expires=Thu, 01 Jan 1970 00:00:00 UTC;';
|
document.cookie = 'luciaToken=; expires=Thu, 01 Jan 1970 00:00:00 UTC;';
|
||||||
|
|
||||||
this.isLoggedIn = false;
|
this.isLoggedIn = false;
|
||||||
|
deleteCookie("isLuciaLoggedIn");
|
||||||
|
|
||||||
this.$router.push('/login');
|
this.$router.push('/login');
|
||||||
},
|
},
|
||||||
|
|||||||
29
src/utils/cookieUtil.js
Normal file
29
src/utils/cookieUtil.js
Normal file
@@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
@@ -20,6 +20,7 @@ import Loading from '@/components/Loading.vue';
|
|||||||
import { leaveFilter, leaveConformance } from '@/module/alertModal.js';
|
import { leaveFilter, leaveConformance } from '@/module/alertModal.js';
|
||||||
import PageAdminStore from '@/stores/pageAdmin.js';
|
import PageAdminStore from '@/stores/pageAdmin.js';
|
||||||
import LoginStore from "@/stores/login.js";
|
import LoginStore from "@/stores/login.js";
|
||||||
|
import { getCookie } from "../utils/cookieUtil.js";
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: 'MainContainer',
|
name: 'MainContainer',
|
||||||
@@ -67,7 +68,7 @@ export default {
|
|||||||
beforeRouteEnter(to, from, next) {
|
beforeRouteEnter(to, from, next) {
|
||||||
// 重新整理畫面以及第一次進入網頁時,beforeRouteEnter這個hook會被執行,然而beforeRouteUpdate不會被執行
|
// 重新整理畫面以及第一次進入網頁時,beforeRouteEnter這個hook會被執行,然而beforeRouteUpdate不會被執行
|
||||||
const loginStore = LoginStore();
|
const loginStore = LoginStore();
|
||||||
if (!loginStore.isLoggedIn) {
|
if (!getCookie("isLuciaLoggedIn")) {
|
||||||
next({
|
next({
|
||||||
path: '/login',
|
path: '/login',
|
||||||
query: {
|
query: {
|
||||||
|
|||||||
Reference in New Issue
Block a user