#[cfg(target_os = "android")]
#[manganis::ffi("src/android/auth")]
unsafe extern "Kotlin" {
pub type AuthPlugin;
}
#[cfg(target_os = "android")]
const AUTH_CLASS: &str = "dev.dioxus.g3_native_plugins.auth.AuthPlugin";
#[cfg(target_os = "android")]
type AuthHandle = crate::android_bridge::AndroidPlugin;
#[cfg(target_os = "ios")]
type AuthHandle = AuthPlugin;
#[cfg(target_os = "ios")]
#[manganis::ffi("src/ios")]
#[allow(missing_docs)]
unsafe extern "Swift" {
pub type AuthPlugin;
pub fn startAppleAuthFromRust(this: &AuthPlugin, requestJson: String) -> Option<String>;
pub fn getPendingResult(this: &AuthPlugin) -> Option<String>;
pub fn getAuthState(this: &AuthPlugin) -> Option<String>;
}
#[cfg(any(target_os = "android", target_os = "ios"))]
pub struct Auth {
plugin: Option<AuthHandle>,
}
#[cfg(target_os = "macos")]
pub struct Auth;
#[cfg(any(target_os = "android", target_os = "ios"))]
impl Auth {
pub(crate) fn new() -> Self {
Self { plugin: None }
}
fn get_plugin(&mut self) -> Result<&AuthHandle, String> {
if self.plugin.is_none() {
#[cfg(target_os = "android")]
let created = AuthHandle::new(AUTH_CLASS)?;
#[cfg(target_os = "ios")]
let created = AuthPlugin::new()
.map_err(|error| format!("Failed to create AuthPlugin: {error:?}"))?;
self.plugin = Some(created);
}
Ok(self.plugin.as_ref().unwrap())
}
#[cfg(target_os = "android")]
pub fn start_google_auth(&mut self, server_client_id: &str) -> Result<(), String> {
if server_client_id.trim().is_empty() {
return Err("A Google server client id is required for sign-in".to_string());
}
let plugin = self.get_plugin()?;
plugin.call_unit_str("startGoogleAuthFromRust", server_client_id)
}
#[cfg(target_os = "ios")]
pub fn start_apple_auth(&mut self, state: &str, nonce: &str) -> Result<(), String> {
if state.trim().is_empty() || nonce.trim().is_empty() {
return Err("Apple sign-in requires a non-empty state and nonce".to_string());
}
let request = serde_json::json!({ "state": state, "nonce": nonce }).to_string();
let plugin = self.get_plugin()?;
_ = startAppleAuthFromRust(plugin, request)?;
Ok(())
}
pub fn poll_auth_result(&mut self) -> Result<Option<String>, String> {
let plugin = self.get_plugin()?;
#[cfg(target_os = "android")]
return plugin.call_string("getPendingResult");
#[cfg(target_os = "ios")]
return Ok(getPendingResult(plugin)?);
}
pub fn is_auth_awaiting(&mut self) -> bool {
let Ok(plugin) = self.get_plugin() else {
return false;
};
#[cfg(target_os = "android")]
let state = plugin.call_string("getAuthState").ok().flatten();
#[cfg(target_os = "ios")]
let state = getAuthState(plugin).ok().flatten();
state.as_deref() == Some("awaiting")
}
}
#[cfg(target_os = "macos")]
impl Auth {
pub(crate) fn new() -> Self {
Self
}
pub fn start_apple_auth(&mut self, _state: &str, _nonce: &str) -> Result<(), String> {
Ok(())
}
pub fn poll_auth_result(&mut self) -> Result<Option<String>, String> {
Ok(None)
}
pub fn is_auth_awaiting(&mut self) -> bool {
false
}
}