From 6fb78970927f69bcb7d89e2f6101708291b195d3 Mon Sep 17 00:00:00 2001 From: Huang Xin Date: Fri, 11 Jul 2025 11:28:43 +0800 Subject: [PATCH] chore: flatpak with custom oauth (#1574) --- apps/readest-app/src-tauri/Cargo.toml | 3 +-- apps/readest-app/src-tauri/src/lib.rs | 28 ++++++++------------------ apps/readest-app/src/app/auth/page.tsx | 23 ++++++++++++++++++--- 3 files changed, 29 insertions(+), 25 deletions(-) diff --git a/apps/readest-app/src-tauri/Cargo.toml b/apps/readest-app/src-tauri/Cargo.toml index 0b78c305..65b10682 100644 --- a/apps/readest-app/src-tauri/Cargo.toml +++ b/apps/readest-app/src-tauri/Cargo.toml @@ -30,8 +30,7 @@ reqwest = { version = "0.12", default-features = false, features = [ "json", "stream", ] } -# FIXME: remove the devtools feature in production -tauri = { version = "2.5.1", features = [ "protocol-asset", "devtools"] } +tauri = { version = "2.5.1", features = [ "protocol-asset" ] } tauri-build = "2.3.0" tauri-plugin-log = "2.4.0" tauri-plugin-fs = "2.2.1" diff --git a/apps/readest-app/src-tauri/src/lib.rs b/apps/readest-app/src-tauri/src/lib.rs index 37983aa7..4c90b8ea 100644 --- a/apps/readest-app/src-tauri/src/lib.rs +++ b/apps/readest-app/src-tauri/src/lib.rs @@ -105,6 +105,11 @@ async fn start_server(window: Window) -> Result { .map_err(|err| err.to_string()) } +#[tauri::command] +fn get_environment_variable(name: &str) -> String { + std::env::var(String::from(name)).unwrap_or(String::from("")) +} + #[derive(Clone, serde::Serialize)] #[allow(dead_code)] struct Payload { @@ -121,6 +126,7 @@ pub fn run() { start_server, download_file, upload_file, + get_environment_variable, #[cfg(target_os = "macos")] macos::safari_auth::auth_with_safari, #[cfg(target_os = "macos")] @@ -220,28 +226,10 @@ pub fn run() { }); } - #[cfg(target_os = "windows")] + #[cfg(any(target_os = "windows", target_os = "linux"))] { use tauri_plugin_deep_link::DeepLinkExt; - app.deep_link().register_all()?; - } - - #[cfg(target_os = "linux")] - { - fn has_xdg_mime() -> bool { - std::process::Command::new("which") - .arg("xdg-mime") - .output() - .map(|output| output.status.success()) - .unwrap_or(false) - } - - if has_xdg_mime() { - use tauri_plugin_deep_link::DeepLinkExt; - app.deep_link().register_all()?; - } else { - println!("xdg-mime not found; skipping deep link setup on Linux."); - } + let _ = app.deep_link().register_all(); } if let Err(e) = app.handle().plugin( diff --git a/apps/readest-app/src/app/auth/page.tsx b/apps/readest-app/src/app/auth/page.tsx index 108f1925..4d59e363 100644 --- a/apps/readest-app/src/app/auth/page.tsx +++ b/apps/readest-app/src/app/auth/page.tsx @@ -22,6 +22,7 @@ import { getBaseUrl, isTauriAppPlatform } from '@/services/environment'; import { onOpenUrl } from '@tauri-apps/plugin-deep-link'; import { start, cancel, onUrl, onInvalidUrl } from '@fabianlars/tauri-plugin-oauth'; import { openUrl } from '@tauri-apps/plugin-opener'; +import { invoke } from '@tauri-apps/api/core'; import { handleAuthCallback } from '@/helpers/auth'; import { getAppleIdAuth, Scope } from './utils/appleIdAuth'; import { authWithCustomTab, authWithSafari } from './utils/nativeAuth'; @@ -69,15 +70,22 @@ export default function AuthPage() { const { isTrafficLightVisible } = useTrafficLightStore(); const { settings, setSettings, saveSettings } = useSettingsStore(); const [port, setPort] = useState(null); - const isOAuthServerRunning = useRef(false); const [isMounted, setIsMounted] = useState(false); + const isOAuthServerRunning = useRef(false); + const useCustomeOAuth = useRef(false); const headerRef = useRef(null); useTheme({ systemUIVisible: false }); const getTauriRedirectTo = (isOAuth: boolean) => { - if (process.env.NODE_ENV === 'production' || appService?.isMobileApp || USE_APPLE_SIGN_IN) { + // For custom OAuth mode, use a local server to handle the OAuth callback + // This is useful for development or some sandboxed environments like Flatpak + // where custom URL schemes are not supported + if ( + !useCustomeOAuth.current && + (process.env.NODE_ENV === 'production' || appService?.isMobileApp || USE_APPLE_SIGN_IN) + ) { if (appService?.isMobileApp) { return isOAuth ? DEEPLINK_CALLBACK : WEB_AUTH_CALLBACK; } @@ -171,7 +179,10 @@ export default function AuthPage() { const startTauriOAuth = async () => { try { - if (process.env.NODE_ENV === 'production' || appService?.isMobileApp || USE_APPLE_SIGN_IN) { + if ( + !useCustomeOAuth.current && + (process.env.NODE_ENV === 'production' || appService?.isMobileApp || USE_APPLE_SIGN_IN) + ) { const { getCurrentWindow } = await import('@tauri-apps/api/window'); const currentWindow = getCurrentWindow(); currentWindow.listen('single-instance', ({ event, payload }) => { @@ -280,6 +291,12 @@ export default function AuthPage() { if (isOAuthServerRunning.current) return; isOAuthServerRunning.current = true; + invoke('get_environment_variable', { name: 'USE_CUSTOM_OAUTH' }).then((value) => { + if (value === 'true') { + useCustomeOAuth.current = true; + } + }); + startTauriOAuth(); return () => { isOAuthServerRunning.current = false;