立项配置页初步优化
This commit is contained in:
@@ -4,8 +4,10 @@ import { login as apiLogin, fetchMe } from "../api/auth";
|
||||
import { setToken, clearToken, getToken } from "../utils/auth";
|
||||
import type { UserInfo } from "../types/api";
|
||||
import { useStudyStore } from "./study";
|
||||
import { useSessionStore } from "./session";
|
||||
|
||||
export const useAuthStore = defineStore("auth", () => {
|
||||
const LAST_LOGIN_EMAIL_KEY = "ctms_last_login_email";
|
||||
const token = ref<string | null>(getToken());
|
||||
const user = ref<UserInfo | null>(null);
|
||||
const loading = ref(false);
|
||||
@@ -17,6 +19,9 @@ export const useAuthStore = defineStore("auth", () => {
|
||||
const { data } = await apiLogin({ email, password });
|
||||
token.value = data.access_token;
|
||||
setToken(data.access_token);
|
||||
// 避免锁屏态下 fetchMe 被请求拦截
|
||||
useSessionStore().unlock();
|
||||
localStorage.setItem(LAST_LOGIN_EMAIL_KEY, email);
|
||||
await fetchMeAction();
|
||||
forceLogin.value = false;
|
||||
} finally {
|
||||
@@ -27,6 +32,9 @@ export const useAuthStore = defineStore("auth", () => {
|
||||
const fetchMeAction = async () => {
|
||||
const { data } = await fetchMe();
|
||||
user.value = data;
|
||||
if (data?.email) {
|
||||
localStorage.setItem(LAST_LOGIN_EMAIL_KEY, data.email);
|
||||
}
|
||||
return data;
|
||||
};
|
||||
|
||||
|
||||
@@ -6,7 +6,9 @@ export type LockReason = "idle" | "extend_failed" | "token_invalid";
|
||||
export const useSessionStore = defineStore("session", () => {
|
||||
const locked = ref(false);
|
||||
const lockReason = ref<LockReason | null>(null);
|
||||
const lockEmail = ref<string>("");
|
||||
const unlockAttempts = ref(0);
|
||||
const lockAutoLogoutDeadlineAt = ref(0);
|
||||
const lastUserActiveAt = ref(Date.now());
|
||||
const lastNetworkActiveAt = ref(Date.now());
|
||||
const lastExtendAt = ref(0);
|
||||
@@ -19,14 +21,18 @@ export const useSessionStore = defineStore("session", () => {
|
||||
lastNetworkActiveAt.value = ts;
|
||||
};
|
||||
|
||||
const lock = (reason: LockReason) => {
|
||||
const lock = (reason: LockReason, email?: string, autoLogoutDeadlineAt?: number) => {
|
||||
locked.value = true;
|
||||
lockReason.value = reason;
|
||||
lockEmail.value = (email || "").trim();
|
||||
lockAutoLogoutDeadlineAt.value = autoLogoutDeadlineAt || 0;
|
||||
};
|
||||
|
||||
const unlock = () => {
|
||||
locked.value = false;
|
||||
lockReason.value = null;
|
||||
lockEmail.value = "";
|
||||
lockAutoLogoutDeadlineAt.value = 0;
|
||||
unlockAttempts.value = 0;
|
||||
};
|
||||
|
||||
@@ -41,6 +47,8 @@ export const useSessionStore = defineStore("session", () => {
|
||||
return {
|
||||
locked,
|
||||
lockReason,
|
||||
lockEmail,
|
||||
lockAutoLogoutDeadlineAt,
|
||||
unlockAttempts,
|
||||
lastUserActiveAt,
|
||||
lastNetworkActiveAt,
|
||||
|
||||
Reference in New Issue
Block a user