authyra_flutter 0.3.0
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.
This package re-exports authyra and adds:
SecureAuthStorage: sessions in the iOS/macOS Keychain and Android Keystore, viaflutter_secure_storage.AuthyraScope: provides the instance to the widget tree and refreshes when the app returns to the foreground, if the token is due.AuthStateBuilderandAuthStateListenable: rebuild on sign-in, sign-out and account switch; plug intogo_routerredirects.OAuth2Provider: authorization code with PKCE,stateandnonce, 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 #
- Quick start
- Widgets and routing
- Storage, including the iOS reinstall caveat
- Native Google and Apple sign-in
License #
MIT