# Sign-in for mobile apps
Source: https://sovaria.in/docs/mobile-auth

Provider sign-in and email links that open your app (authorization code + PKCE), universal links hosted by Sovaria, device sessions and refresh-token rotation.

## Redirect URIs

A sign-in or email link may only open a redirect URI listed on one of your **registered apps** — exact matches, no wildcards. Two kinds work:

- Your app's own scheme: `com.example.app://auth-callback`.
- A universal link / App Link on your project host: `https://<ref>.sovarias.in/auth/v1/app-callback`. Sovaria serves `/.well-known/apple-app-site-association` and `/.well-known/assetlinks.json` there from your registered apps, so you only add the Associated Domains entitlement (`applinks:<ref>.sovarias.in`) or the Android intent filter.

> **PKCE is required:** Any app on a phone can claim a custom scheme, so redirects into an app must carry a PKCE `code_challenge`; without one the request fails with `PKCE_REQUIRED`. The SDKs add it for you. (Authentication › Sessions lets you turn this off, but don't.)

## Sign in with Google, Apple, GitHub…

Dart:
```dart
final start = await sovaria.auth.getOAuthSignInUrl('google', redirectTo: 'com.example.app://auth-callback');
final result = await FlutterWebAuth2.authenticate(url: start.url.toString(), callbackUrlScheme: 'com.example.app');
await sovaria.auth.handleDeepLink(Uri.parse(result));   // exchanges ?code=…&type=sso with the verifier
```

HTTP:
```http
GET /api/v1/auth/sso/google/authorize?project_id=…&redirect_to=com.example.app://auth-callback&code_challenge=<S256>&code_challenge_method=S256&mode=json
→ { "authorization_url": "https://accounts.google.com/…" }
# provider → com.example.app://auth-callback?code=…&type=sso
POST /api/v1/auth/sso/exchange   { "code": "…", "code_verifier": "…" }
```

## Magic links, email codes and password reset

Dart:
```dart
await sovaria.auth.sendMagicLink(email, redirectTo: 'com.example.app://auth-callback');
AppLinks().uriLinkStream.listen((uri) => sovaria.auth.handleDeepLink(uri));     // signs in
// or the 6-digit code from the same email:
await sovaria.auth.verifyEmailCode(email: email, code: code);

await sovaria.auth.resetPasswordForEmail(email, redirectTo: 'com.example.app://reset');
final link = await sovaria.auth.handleDeepLink(uri);                              // type == 'recovery'
await sovaria.auth.confirmPasswordReset(token: link!.token!, newPassword: newPassword);
```

A link sent with a challenge only works with the verifier the requesting install holds — opening it on another device (or in another app) fails with `PKCE_VERIFIER_MISMATCH`. `handleDeepLink` signs in only from a sign-in this install started (its verifier is waiting): a sign-in link it didn't start — another app, a web page or a QR code can open your app's links — signs nobody in and returns `requiresConfirmation` with the `token`, so the app can ask the user and call `verifyMagicLink(token)` itself (or pass `allowUnsolicited` for the old behaviour). SMS codes work as before: `sendSmsOtp` / `verifySmsOtp`.

## Devices and sessions

Pass a stable `device` (id kept in secure storage, platform, name, app version). Every session is tied to it and keeps it across token refreshes.

Dart:
```dart
final devices = await sovaria.auth.listDevices();   // current: true marks this phone
await sovaria.auth.signOutDevice(deviceId);           // ends its sessions now, removes its push tokens
```

HTTP:
```http
GET    /api/v1/auth/devices
DELETE /api/v1/auth/devices/{device_id}
# team / server key: an app user's devices
GET    /api/v1/projects/{project_id}/users/{user_id}/devices
DELETE /api/v1/projects/{project_id}/users/{user_id}/devices/{device_id}
```

- Refresh tokens rotate on every refresh. Two refreshes racing with the same token within the reuse window (10 s by default, Authentication › Sessions) get the same new pair.
- A rotated refresh token presented again after that window means it was copied: every session of the user ends and they sign in again.
- Device headers: `X-Sovaria-Device-ID`, `X-Sovaria-Device-Name`, `X-Sovaria-Device-Platform`, `X-Sovaria-App-Version`. They label sessions; they never grant anything.
