11
This commit is contained in:
@@ -23,11 +23,13 @@ import Dialog from "primevue/dialog";
|
||||
import DashboardCard from "./components/dashboard/DashboardCard.vue";
|
||||
import App from "./App.vue";
|
||||
import router from "./router";
|
||||
import { useAuthStore } from "./stores/auth.js";
|
||||
import "./styles/main.css";
|
||||
|
||||
const app = createApp(App);
|
||||
|
||||
app.use(createPinia());
|
||||
const pinia = createPinia();
|
||||
app.use(pinia);
|
||||
app.use(router);
|
||||
app.use(PrimeVue, {
|
||||
theme: {
|
||||
@@ -58,4 +60,6 @@ app.component("TabPanel", TabPanel);
|
||||
app.component("DashboardCard", DashboardCard);
|
||||
app.component("Dialog", Dialog);
|
||||
|
||||
useAuthStore(pinia).hydrateRustSession();
|
||||
|
||||
app.mount("#app");
|
||||
|
||||
@@ -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