Dart: Authentication
Sign app users in with passwords, magic links, SMS and email codes, OAuth/SSO and passkeys; keep and refresh the session; handle deep links with the Dart and Flutter SDK.
Sign app users in with passwords, magic links, SMS and email codes, OAuth/SSO and passkeys; keep and refresh the session; handle deep links. Every call runs as the client's signed-in user, so your project's policies and row-level security apply. Types and members are listed in source order; each one links to its HTTP endpoint where it maps to one.
#Types and members
AuthLinkResult
What SovariaAuth.handleDeepLink found in an opened link.
class AuthLinkResultAuthLinkResult.AuthLinkResult
Create an instance.
const AuthLinkResult({required this.type, this.session, this.token, this.error, this.errorDescription})AuthLinkResult.type
sso, magiclink, recovery, signup, email_change_current, email_change_new.
final String typeAuthLinkResult.session
Set when the link signed the user in (sso, magiclink).
final Session? sessionAuthLinkResult.token
For recovery: pass it to SovariaAuth.confirmPasswordReset with the new password.
final String? tokenAuthLinkResult.error
A property.
final String? errorAuthLinkResult.errorDescription
A property.
final String? errorDescriptionOAuthStart
Where to send the user for "Sign in with …" (open it in an auth session / custom tab).
class OAuthStartOAuthStart.OAuthStart
Create an instance.
const OAuthStart(this.url, this.verifier)OAuthStart.url
A property.
final Uri urlOAuthStart.verifier
Kept in the client's storage too; only needed if you exchange the code yourself.
final String verifierSovariaAuth
App-user sign-in for one project.
class SovariaAuthSovariaAuth.SovariaAuth
Create an instance.
SovariaAuth(this._client)SovariaAuth.passkeys
Passkeys (WebAuthn): add one for the signed-in user, sign in with one, list and remove them.
late final SovariaPasskeys passkeysSovariaAuth.currentSession
The signed-in user's session, or null.
Session? get currentSessionSovariaAuth.currentUserId
The signed-in user's id, or null.
String? get currentUserIdSovariaAuth.onAuthStateChange
Sign-in, refresh and sign-out events.
Stream<AuthState> get onAuthStateChangeNew listeners get the current state first.
SovariaAuth.restoreSession
Load the stored session (called by Sovaria.initialize).
Future<Session?> restoreSession()SovariaAuth.currentAccessToken
The token to sign requests with: the session's (refreshed first when near expiry) or null.
Future<String?> currentAccessToken()SovariaAuth.signUp
Create an account and sign in.
Future<Map<String, Object?>> signUp({required String email, required String password, String name = ""})When the project requires email confirmation the result has email_verification_required: true and there is no session until the user confirms.
SovariaAuth.signInWithPassword
Future<Session> signInWithPassword({required String email, required String password, String? captchaToken})SovariaAuth.signInAnonymously
A guest user (when Anonymous sign-in is on for the project).
Future<Session> signInAnonymously()SovariaAuth.verifyMfa
Finish sign-in with a TOTP (or SMS) code after mfaRequired.
Future<Session> verifyMfa(String code, {String method = ""})SovariaAuth.sendSmsOtp
Text a 6-digit code to phone (E.164, e.g.
Future<void> sendSmsOtp(String phone)+919876543210).
SovariaAuth.verifySmsOtp
Future<Session> verifySmsOtp({required String phone, required String code})SovariaAuth.sendMagicLink
Email a sign-in link plus a 6-digit code.
Future<void> sendMagicLink(String email, {String? redirectTo, bool? pkce, String? captchaToken})A link that opens your app (redirectTo with your app's scheme, or a registered app link with pkce) is bound to this install with PKCE.
SovariaAuth.verifyMagicLink
Sign in with the token from a magic link (usually via handleDeepLink).
Future<Session> verifyMagicLink(String token, {String? codeVerifier})SovariaAuth.verifyEmailCode
Sign in with the 6-digit code from the sign-in email.
Future<Session> verifyEmailCode({required String email, required String code})SovariaAuth.resetPasswordForEmail
Email a password-reset link (PKCE-bound when it opens your app).
Future<void> resetPasswordForEmail(String email, {String? redirectTo, bool? pkce})SovariaAuth.confirmPasswordReset
Set the new password with the token from a type=recovery link.
Future<void> confirmPasswordReset({required String token, required String newPassword, String? codeVerifier})SovariaAuth.getOAuthSignInUrl
The URL to open for "Sign in with provider" (Google, Apple, GitHub, Microsoft, your OIDC provider…).
Future<OAuthStart> getOAuthSignInUrl(String provider, {String? redirectTo})The provider sends the user back to redirectTo (your app's scheme or a registered app link) with ?code=…&type=sso; pass that URI to handleDeepLink.
With flutter_web_auth_2:
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));SovariaAuth.exchangeCodeForSession
Exchange the one-time code from the redirect for a session.
Future<Session> exchangeCodeForSession(String code, {String? codeVerifier})SovariaAuth.listProviders
Enabled sign-in providers for your sign-in screen.
Future<List<Map<String, Object?>>> listProviders()SovariaAuth.handleDeepLink
Handle a deep link / universal link that opened the app: provider sign-in (type=sso), magic links, email confirmations; for recovery the token is returned for the new-password screen.
Future<AuthLinkResult?> handleDeepLink(Uri uri)Returns null when the link isn't an auth link.
SovariaAuth.refreshSession
Rotate the refresh token now.
Future<Session?> refreshSession({bool silent = false})Concurrent callers share one request. A refresh token presented twice (stolen copy) makes the server end every session; the user must sign in again.
SovariaAuth.getUser
The signed-in user's profile.
Future<Map<String, Object?>> getUser()SovariaAuth.listDevices
Devices this user is signed in on (current: true is this one).
Future<List<Map<String, Object?>>> listDevices()SovariaAuth.signOutDevice
Sign another device out (its sessions end now and its push tokens are removed).
Future<void> signOutDevice(String deviceId)SovariaAuth.signOut
Sign out on this device (the refresh token is revoked; requests fall back to the anon key).
Future<void> signOut()SovariaAuth.dispose
Future<void> dispose()SovariaPasskeys
Passkey calls. Flutter has no built-in WebAuthn API: run the platform ceremony with a plugin (for example passkeys or credential_manager), giving it the options JSON from a start… call and sending the credential JSON it returns to the matching finish… call.
class SovariaPasskeysSovariaPasskeys.startRegistration
Registration options (WebAuthn JSON) for a new passkey for the signed-in user.
Future<Map<String, Object?>> startRegistration({String name = ""})SovariaPasskeys.finishRegistration
Save the passkey the authenticator created (its WebAuthn JSON).
Future<Map<String, Object?>> finishRegistration(Map<String, Object?> credential, {String name = ""})SovariaPasskeys.startSignIn
Sign-in options.
Future<PasskeySignInStart> startSignIn({String? userId})Pass userId to offer only that user's passkeys.
SovariaPasskeys.finishSignIn
Verify the signed challenge and sign the user in (two-factor when the device checked the user).
Future<Session> finishSignIn(String challengeId, Map<String, Object?> credential)SovariaPasskeys.list
The signed-in user's passkeys: id, name, created_at, last_used_at.
Future<List<Map<String, Object?>>> list()SovariaPasskeys.remove
Remove one of the signed-in user's passkeys.
Future<void> remove(String passkeyId)PasskeySignInStart
What SovariaPasskeys.startSignIn returns: pass options to the authenticator, then challengeId and its credential to SovariaPasskeys.finishSignIn.
class PasskeySignInStartPasskeySignInStart.PasskeySignInStart
Create an instance.
const PasskeySignInStart(this.challengeId, this.options)PasskeySignInStart.challengeId
A property.
final String challengeIdPasskeySignInStart.options
A property.
final Map<String, Object?> optionsSession
A signed-in app user's tokens.
class SessionSession.Session
Create an instance.
Session({required this.accessToken, required this.refreshToken, required this.expiresAt, this.raw = const {}})Session.accessToken
A property.
final String accessTokenSession.refreshToken
A property.
final String refreshTokenSession.expiresAt
A property.
final DateTime expiresAtSession.raw
The full token response (actor lane, MFA flags, …).
final Map<String, Object?> rawSession.userId
The user id (sub of the access token).
String? get userIdSession.claims
The access token's claims (not verified: the server verifies it on every request).
Map<String, Object?> get claimsSession.expiresWithin
bool expiresWithin(Duration margin, {DateTime? now})Session.Session.fromTokens
Create an instance.
factory Session.fromTokens(Map<String, Object?> tokens, {DateTime? now})Session.toJson
Map<String, Object?> toJson()Session.fromJson
static Session? fromJson(Object? json)SessionStorage
Where the client keeps the session and in-flight PKCE verifiers.
Use secure storage in apps (Keychain / Keystore), for example with flutter_secure_storage:
abstract class SessionStorageSessionStorage.read
Future<String?> read(String key)SessionStorage.write
Future<void> write(String key, String value)SessionStorage.delete
Future<void> delete(String key)MemorySessionStorage
Keeps everything in memory (tests, server-side Dart, or apps that sign in every launch).
class MemorySessionStorage implements SessionStorageMemorySessionStorage.values
A property.
final Map<String, String> valuesMemorySessionStorage.read
Future<String?> read(String key)MemorySessionStorage.write
Future<void> write(String key, String value)MemorySessionStorage.delete
Future<void> delete(String key)SovariaDevice
The device the app runs on. Sessions are grouped by it (auth.listDevices, sign a device out) and push tokens are tied to it. Generate id once and keep it in secure storage.
class SovariaDeviceSovariaDevice.SovariaDevice
Create an instance.
const SovariaDevice({required this.id, this.name, this.platform, this.appVersion})SovariaDevice.id
A property.
final String idSovariaDevice.name
A property.
final String? nameSovariaDevice.platform
ios, android, web, macos, windows, linux or other.
final String? platformSovariaDevice.appVersion
A property.
final String? appVersionSovariaDevice.headers
A property.
Map<String, String> get headersAuthEvent
Auth events for auth.onAuthStateChange.
enum AuthEventValues: initialSession, signedIn, tokenRefreshed, signedOut, mfaRequired.
AuthState
class AuthStateAuthState.AuthState
Create an instance.
const AuthState(this.event, this.session)AuthState.event
A property.
final AuthEvent eventAuthState.session
A property.
final Session? sessionPkce
A PKCE pair (RFC 7636, S256): keep verifier on the device, send challenge with the request.
class PkcePkce.verifier
A property.
final String verifierPkce.challenge
A property.
final String challengePkce.Pkce.generate
A fresh pair from 32 random bytes (a 43-character verifier).
factory Pkce.generate([Random? random])Pkce.challengeFor
BASE64URL(SHA-256(verifier)) without padding.
static String challengeFor(String verifier)isAppRedirect
A redirect into an app (custom scheme such as com.example.app://callback).
bool isAppRedirect(String? url)