Leverage iOS' ASWebAuthenticationSession and Android's Custom Tabs to authenticate users in your Tauri app.
npm install tauri-plugin-web-auth-apimanaf941 / manaaaaaaaf> [!WARNING]
> I have NOT tested this plugin in conjunction with the Tauri Deep Linking plugin. It MAY be incompatible. I cannot guarantee this will work with your setup.
> [!NOTE]
> This plugin only works on iOS and Android. I have no plan to bring it to Desktop. I will answer pull requests though
src-tauri/src/lib.rsrs
#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
tauri::Builder::default()
// this line initializes the plugin
.plugin(tauri_plugin_web_auth::init())
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
`src-tauri/capabilities/default.json
`json
{
"permissions": [
"web-auth:default",
"web-auth:allow-authenticate"
]
}`index.ts
`ts
import { authenticate } from 'tauri-plugin-web-auth-api'// Example: Authenticate to google
// scheme is
iOS URL scheme in Google Cloud Console
const scheme = "com.googleusercontent.apps.807645982538-7ib8rr1tkjkeb3vr0u1lk7gs6ip3b0jl"
const url = new URL("https://accounts.google.com/o/oauth2/v2/auth")
url.searchParams.set("response_type", "code")
// client id is Client ID under Google Cloud Console, it's basically the reverse scheme
url.searchParams.set("client_id", "807645982538-7ib8rr1tkjkeb3vr0u1lk7gs6ip3b0jl.apps.googleusercontent.com")
url.searchParams.set("redirect_uri", scheme+":/")
// scope must be separated by spaces
url.searchParams.set("scope", "email")
s
const res = await authenticate({
url: url.toString(),
callbackScheme: scheme,
})const callback_url = new URL(res.callbackUrl)
// You can then exchange this code with your backend or with google to
// get the user's informations
console.log(
Got code: ${callback_url.searchParams.get("code")})
`Setup
iOS
iOS should work out of the box, no need to modify any configuration files.Android
Android is bit more annoying. You need to add the callback scheme to your src-tauri/gen/android/app/src/AndroidManifest.xml file.
`xml
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|smallestScreenSize|screenLayout|uiMode"
android:launchMode="singleTask"
android:label="@string/main_activity_title"
android:name=".MainActivity"
android:exported="true">
...
``