Providers
Custom providers
Write a provider for magic links, one-time codes, passkeys or any other way to sign in.
A provider is a small, stateless class. It authenticates, optionally refreshes and revokes; Authyra owns the session.
final class OtpParams extends AuthSignInParams {
const OtpParams({required this.challengeId, required this.code});
final String challengeId;
final String code;
}
final class OtpProvider extends AuthProvider {
const OtpProvider(this.api);
final MyAuthApi api;
@override
String get id => 'otp';
@override
Future<AuthSignInResult> signIn(AuthSignInParams? params) async {
if (params is! OtpParams) {
throw SignInFailedException(id, 'Expected OtpParams');
}
final response = await api.verifyCode(params.challengeId, params.code);
if (response == null) throw SignInFailedException(id, 'Invalid code');
return AuthSignInResult(user: response.user, tokens: response.tokens);
}
@override
bool get supportsRefresh => true;
@override
Future<AuthTokens> refresh(AuthSession session) =>
api.refresh(session.tokens!.refreshToken!);
@override
Future<void> signOut(AuthSession session) =>
api.revoke(session.tokens?.refreshToken);
}
The contract
| Member | Rules |
|---|---|
id | Unique within the instance, and stable across releases: saved sessions refer to it. |
signIn(params) | Return an AuthSignInResult. Throw SignInCancelledException when the user aborts and SignInFailedException when authentication fails. Other errors are wrapped in SignInFailedException. |
supportsRefresh | true when refresh is implemented. |
refresh(session) | Return new tokens. Throw RefreshRevokedException only when the server definitively rejects the refresh token. Never called concurrently for the same session, and always with the latest tokens. |
signOut(session) | Revoke server-side if you can. Failures are logged and never block the local sign-out. |
Keep the class free of mutable state: everything about a sign-in lives in the signIn call, everything about a session in the AuthSession Authyra passes you.
Testing it
A provider is plain Dart: call signIn and refresh directly in unit tests, then register it with an Authyra built on InMemoryAuthStorage to test it end to end. See Testing.