11
This commit is contained in:
@@ -1,9 +1,21 @@
|
||||
import { defineStore } from "pinia";
|
||||
import { invoke } from "@tauri-apps/api/core";
|
||||
import { loginApi, logoutApi, registerApi } from "../api/auth.js";
|
||||
|
||||
const TOKEN_KEY = "aiclient_token";
|
||||
const USER_KEY = "aiclient_current_user";
|
||||
|
||||
async function syncAuthSessionToRust(token, user) {
|
||||
try {
|
||||
await invoke("sync_auth_session", {
|
||||
token: token || "",
|
||||
user: user ?? null,
|
||||
});
|
||||
} catch {
|
||||
// 浏览器独立预览或未就绪的非 Tauri 环境
|
||||
}
|
||||
}
|
||||
|
||||
function applyTokenResponse(store, res, fallbackMessage) {
|
||||
if (!res.ok) {
|
||||
return { ok: false, message: res.message || fallbackMessage };
|
||||
@@ -45,6 +57,7 @@ export const useAuthStore = defineStore("auth", {
|
||||
this.currentUser = user;
|
||||
localStorage.setItem(TOKEN_KEY, accessToken);
|
||||
localStorage.setItem(USER_KEY, JSON.stringify(user));
|
||||
syncAuthSessionToRust(accessToken, user);
|
||||
},
|
||||
|
||||
clearSession() {
|
||||
@@ -52,6 +65,12 @@ export const useAuthStore = defineStore("auth", {
|
||||
this.currentUser = null;
|
||||
localStorage.removeItem(TOKEN_KEY);
|
||||
localStorage.removeItem(USER_KEY);
|
||||
syncAuthSessionToRust("", null);
|
||||
},
|
||||
|
||||
/** 将当前内存中的会话同步到 Rust(启动时 localStorage 已恢复后调用) */
|
||||
hydrateRustSession() {
|
||||
syncAuthSessionToRust(this.token, this.currentUser);
|
||||
},
|
||||
|
||||
async register({ username, password, confirmPassword }) {
|
||||
|
||||
Reference in New Issue
Block a user