11
This commit is contained in:
37
src-tauri/src/auth_session.rs
Normal file
37
src-tauri/src/auth_session.rs
Normal file
@@ -0,0 +1,37 @@
|
||||
//! 与前端 localStorage 会话镜像:通过 `sync_auth_session` 命令更新。
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::sync::Mutex;
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct AuthUser {
|
||||
pub id: i64,
|
||||
pub username: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Default)]
|
||||
struct Inner {
|
||||
token: String,
|
||||
user: Option<AuthUser>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct AuthSession {
|
||||
inner: Mutex<Inner>,
|
||||
}
|
||||
|
||||
impl AuthSession {
|
||||
pub fn set(&self, token: String, user: Option<AuthUser>) {
|
||||
let mut g = self.inner.lock().expect("auth session mutex poisoned");
|
||||
g.token = token;
|
||||
g.user = user;
|
||||
}
|
||||
|
||||
pub fn token(&self) -> String {
|
||||
self.inner.lock().expect("auth session mutex poisoned").token.clone()
|
||||
}
|
||||
|
||||
pub fn user(&self) -> Option<AuthUser> {
|
||||
self.inner.lock().expect("auth session mutex poisoned").user.clone()
|
||||
}
|
||||
}
|
||||
12
src-tauri/src/commands/auth.rs
Normal file
12
src-tauri/src/commands/auth.rs
Normal file
@@ -0,0 +1,12 @@
|
||||
use tauri::State;
|
||||
|
||||
use crate::auth_session::{AuthSession, AuthUser};
|
||||
|
||||
#[tauri::command]
|
||||
pub fn sync_auth_session(
|
||||
session: State<'_, AuthSession>,
|
||||
token: String,
|
||||
user: Option<AuthUser>,
|
||||
) {
|
||||
session.set(token, user);
|
||||
}
|
||||
@@ -1 +1,2 @@
|
||||
pub mod auth;
|
||||
pub mod quickjs;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
pub mod asr;
|
||||
pub mod auth_session;
|
||||
pub mod chat;
|
||||
pub mod commands;
|
||||
pub mod ffmpeg;
|
||||
@@ -13,6 +14,7 @@ pub fn run() {
|
||||
let _ = env_logger::try_init();
|
||||
|
||||
tauri::Builder::default()
|
||||
.manage(auth_session::AuthSession::default())
|
||||
.plugin(tauri_plugin_single_instance::init(|app, _args, _cwd| {
|
||||
if let Some(window) = app.get_webview_window("main") {
|
||||
let _ = window.unminimize();
|
||||
@@ -22,6 +24,7 @@ pub fn run() {
|
||||
}))
|
||||
.plugin(tauri_plugin_opener::init())
|
||||
.invoke_handler(tauri::generate_handler![
|
||||
commands::auth::sync_auth_session,
|
||||
commands::quickjs::run_quickjs_script,
|
||||
commands::quickjs::list_quickjs_scripts,
|
||||
])
|
||||
|
||||
Reference in New Issue
Block a user