use tauri::{
plugin::{Builder, TauriPlugin},
Runtime,
};
#[cfg(any(target_os = "macos", target_os = "ios"))]
mod apple;
#[cfg(target_os = "android")]
mod android;
#[cfg(target_os = "android")]
const PLUGIN_IDENTIFIER: &str = "app.tauri.auth_session";
#[cfg(target_os = "android")]
use tauri::{AppHandle, Manager};
#[cfg(target_os = "android")]
struct MobilePluginHandle<R: Runtime>(tauri::plugin::PluginHandle<R>);
pub fn init<R: Runtime>() -> TauriPlugin<R> {
Builder::new("auth-session")
.setup(|app, api| {
#[cfg(target_os = "android")]
{
let handle = api.register_android_plugin(PLUGIN_IDENTIFIER, "AuthSessionPlugin")?;
app.manage(MobilePluginHandle(handle));
}
#[cfg(not(target_os = "android"))]
{
let _ = (app, api);
}
Ok(())
})
.invoke_handler(tauri::generate_handler![start])
.build()
}
#[cfg(any(target_os = "macos", target_os = "ios"))]
#[tauri::command]
async fn start(
auth_url: String,
callback_url_scheme: String,
ephemeral: Option<bool>,
) -> Result<String, String> {
apple::start_session(auth_url, callback_url_scheme, ephemeral.unwrap_or(false)).await
}
#[cfg(target_os = "android")]
#[tauri::command]
async fn start<R: Runtime>(
app: AppHandle<R>,
auth_url: String,
callback_url_scheme: String,
_ephemeral: Option<bool>,
) -> Result<String, String> {
let handle = app.state::<MobilePluginHandle<R>>();
android::start_session(&handle.0, auth_url, callback_url_scheme).await
}
#[cfg(not(any(target_os = "macos", target_os = "ios", target_os = "android")))]
#[tauri::command]
async fn start(
_auth_url: String,
_callback_url_scheme: String,
_ephemeral: Option<bool>,
) -> Result<String, String> {
Err("In-app auth sessions are only available on Apple and Android platforms".to_string())
}