Skip to main content

authyra 0.3.0 copy "authyra: ^0.3.0" to clipboard
authyra: ^0.3.0 copied to clipboard

The session layer for Dart and Flutter apps with their own backend: persistence, safe token refresh, multi-account and reactive auth state.

authyra #

The session layer for Dart and Flutter apps with their own backend: persistence, safe token refresh, multi-account and reactive auth state.

This is the pure Dart core. Flutter apps should depend on authyra_flutter, which re-exports this package and adds secure storage and widgets.

Documentation · Live demo

What it does #

Your API issues an access token and a refresh token. Authyra:

  • stores the session and restores it at startup,
  • refreshes the access token before it expires and after a 401, one refresh at a time per session, so rotating refresh tokens are never reused,
  • keeps the session through network errors and 5xx, and ends it when the server rejects the refresh token (or, for a session that cannot be refreshed, when its access token is rejected or expires),
  • holds several accounts at once, one of them active,
  • exposes the state (AuthUnknown, AuthSignedIn, AuthSignedOut) and a stream of events.

Usage #

import 'package:authyra/authyra.dart';

final authyra = Authyra(
  storage: InMemoryAuthStorage(), // SecureAuthStorage in Flutter apps.
  providers: [
    CredentialsProvider(
      id: 'api',
      signIn: (credentials) async {
        final json = await api.login(credentials.identifier, credentials.password);
        if (json == null) return null; // Wrong credentials.
        return AuthSignInResult(
          user: AuthUser(id: json['user_id'] as String),
          tokens: AuthTokens.expiringIn(
            Duration(seconds: json['expires_in'] as int),
            accessToken: json['access_token'] as String,
            refreshToken: json['refresh_token'] as String?,
          ),
        );
      },
      refresh: (refreshToken) async {
        // Throw RefreshRevokedException when the server rejects the token
        // (401, invalid_grant). Any other error keeps the session.
        final json = await api.refresh(refreshToken);
        return AuthTokens.expiringIn(
          Duration(seconds: json['expires_in'] as int),
          accessToken: json['access_token'] as String,
          refreshToken: json['refresh_token'] as String?,
        );
      },
    ),
  ],
);

await authyra.start();

await authyra.signIn(
  'api',
  params: const CredentialsSignInParams(identifier: 'ada@example.com', password: '...'),
);

final token = await authyra.accessToken(); // Valid, refreshed if needed.

authyra.states.listen((state) => print(state));

Main API #

Member Purpose
start() / Authyra.initialize(...) Restore stored sessions and begin scheduling refreshes.
signIn(providerId, params:) Sign in and store the session.
accessToken(key:, rejectedToken:) A valid token, refreshed first if needed.
signOut(key:), signOutAll() End one or every session, revoking server-side when the provider supports it.
sessions, switchTo(key) Multi-account.
state, states, events Who is signed in, and what happened.

Providers in this package: CredentialsProvider, and the AuthProvider interface for your own. OAuth 2.0, Google and Apple providers live in the Flutter packages.

Learn more #

License #

MIT

1
likes
150
points
211
downloads

Documentation

Documentation
API reference

Publisher

verified publishermeragix.dev

Weekly Downloads

The session layer for Dart and Flutter apps with their own backend: persistence, safe token refresh, multi-account and reactive auth state.

Homepage
Repository (GitHub)
View/report issues
Contributing

Topics

#authentication #session #oauth #security

License

MIT (license)

Dependencies

clock, logging

More

Packages that depend on authyra