支持开发环境局域网登录测试
This commit is contained in:
+22
-12
@@ -1,6 +1,6 @@
|
||||
import { defineStore } from "pinia";
|
||||
import { ref } from "vue";
|
||||
import { getLoginKey, login as apiLogin, fetchMe } from "../api/auth";
|
||||
import { getLoginKey, login as apiLogin, devLogin, fetchMe } from "../api/auth";
|
||||
import { setToken, clearToken, getToken } from "../utils/auth";
|
||||
import { encryptLoginPayload } from "../utils/loginCrypto";
|
||||
import type { UserInfo } from "../types/api";
|
||||
@@ -9,6 +9,9 @@ import { useSessionStore } from "./session";
|
||||
|
||||
export const useAuthStore = defineStore("auth", () => {
|
||||
const LAST_LOGIN_EMAIL_KEY = "ctms_last_login_email";
|
||||
const allowInsecureDevLogin =
|
||||
import.meta.env.VITE_RUNTIME_ENV === "development" &&
|
||||
import.meta.env.VITE_ALLOW_INSECURE_DEV_LOGIN === "true";
|
||||
const token = ref<string | null>(getToken());
|
||||
const user = ref<UserInfo | null>(null);
|
||||
const loading = ref(false);
|
||||
@@ -17,17 +20,10 @@ export const useAuthStore = defineStore("auth", () => {
|
||||
const login = async (email: string, password: string) => {
|
||||
loading.value = true;
|
||||
try {
|
||||
const { data: loginKey } = await getLoginKey();
|
||||
const ciphertext = await encryptLoginPayload(loginKey.public_key, {
|
||||
email,
|
||||
password,
|
||||
challenge: loginKey.challenge,
|
||||
});
|
||||
const { data } = await apiLogin({
|
||||
key_id: loginKey.key_id,
|
||||
challenge: loginKey.challenge,
|
||||
ciphertext,
|
||||
});
|
||||
const { data } =
|
||||
allowInsecureDevLogin && !window.isSecureContext
|
||||
? await devLogin({ email, password })
|
||||
: await encryptedLogin(email, password);
|
||||
token.value = data.access_token;
|
||||
setToken(data.access_token);
|
||||
// 避免锁屏态下 fetchMe 被请求拦截
|
||||
@@ -44,6 +40,20 @@ export const useAuthStore = defineStore("auth", () => {
|
||||
}
|
||||
};
|
||||
|
||||
const encryptedLogin = async (email: string, password: string) => {
|
||||
const { data: loginKey } = await getLoginKey();
|
||||
const ciphertext = await encryptLoginPayload(loginKey.public_key, {
|
||||
email,
|
||||
password,
|
||||
challenge: loginKey.challenge,
|
||||
});
|
||||
return apiLogin({
|
||||
key_id: loginKey.key_id,
|
||||
challenge: loginKey.challenge,
|
||||
ciphertext,
|
||||
});
|
||||
};
|
||||
|
||||
const fetchMeAction = async () => {
|
||||
const { data } = await fetchMe();
|
||||
user.value = data;
|
||||
|
||||
Reference in New Issue
Block a user