Skip to main content

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

Flutter integration for Authyra: secure session storage, refresh on app resume, auth-state widgets, and OAuth 2.0 / backend-mediated sign-in.

authyra_flutter #

Flutter integration for Authyra, the session layer for apps with their own backend.

Documentation · Live demo

This package re-exports authyra and adds:

  • SecureAuthStorage: sessions in the iOS/macOS Keychain and Android Keystore, via flutter_secure_storage.
  • AuthyraScope: provides the instance to the widget tree and refreshes when the app returns to the foreground, if the token is due.
  • AuthStateBuilder and AuthStateListenable: rebuild on sign-in, sign-out and account switch; plug into go_router redirects.
  • OAuth2Provider: authorization code with PKCE, state and nonce, through the system browser.
  • BackendOAuthProvider: social logins whose OAuth flow runs on your backend (GitHub, GitLab, Discord...).

Install #

flutter pub add authyra_flutter

Usage #

import 'package:authyra_flutter/authyra_flutter.dart';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  final authyra = await Authyra.initialize(
    storage: SecureAuthStorage(),
    providers: [apiProvider], // See the quick start.
  );
  runApp(AuthyraScope(authyra: authyra, child: const App()));
}

class App extends StatelessWidget {
  const App({super.key});

  @override
  Widget build(BuildContext context) => MaterialApp(
    home: AuthStateBuilder(
      builder: (context, state) => switch (state) {
        AuthUnknown() => const SplashScreen(),
        AuthSignedIn(:final user) => HomeScreen(user: user),
        AuthSignedOut() => const SignInScreen(),
      },
    ),
  );
}

From any widget: AuthyraScope.of(context).signIn(...) or .signOut().

OAuth 2.0 with PKCE #

OAuth2Provider(
  id: 'sso',
  config: OAuth2Config(
    authorizationEndpoint: Uri.parse('https://sso.example.com/realms/app/protocol/openid-connect/auth'),
    tokenEndpoint: Uri.parse('https://sso.example.com/realms/app/protocol/openid-connect/token'),
    clientId: 'mobile-app',
    redirectUri: Uri.parse('com.example.app:/oauth-callback'),
  ),
)

The redirect returns to the call that opened the browser through flutter_web_auth_2: no deep-link routing to write. Configure the callback scheme as flutter_web_auth_2 describes for each platform.

GitHub and other backend-driven logins #

Providers like GitHub need a client secret, so the code exchange happens on your backend, which returns your own session tokens. See Backend OAuth.

Learn more #

License #

MIT

1
likes
150
points
205
downloads

Documentation

Documentation
API reference

Publisher

verified publishermeragix.dev

Weekly Downloads

Flutter integration for Authyra: secure session storage, refresh on app resume, auth-state widgets, and OAuth 2.0 / backend-mediated sign-in.

Homepage
Repository (GitHub)
View/report issues
Contributing

Topics

#authentication #session #oauth #security

License

MIT (license)

Dependencies

authyra, clock, crypto, flutter, flutter_secure_storage, flutter_web_auth_2, http, logging

More

Packages that depend on authyra_flutter