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

Libraries

authyra_flutter
Flutter integration for Authyra.