firebase_auth_secure_storage_flutter 0.1.0
firebase_auth_secure_storage_flutter: ^0.1.0 copied to clipboard
Unofficial secure storage for the Firebase Auth session of firebase_auth_providers_dart in Flutter apps: the Keychain, the Android Keystore, libsecret and DPAPI through flutter_secure_storage.
// Keeps the session of a FirebaseUserAuth in the secure store of the platform.
//
// With firebase_auth_dart_platform (the official firebase_auth API), pass the
// same storage to FirebaseAuthDart.use instead:
//
// final SecureAuthStorage storage = SecureAuthStorage();
// FirebaseAuthDart.use(storageFor: (FirebaseApp app) => storage);
//
// Run it against an emulator started with
// `firebase emulators:start --only auth` (project demo-test). On macOS the app
// must be signed: see "macOS" in the README of the package.
import 'package:firebase_auth_providers_dart/firebase_auth_providers_dart.dart';
import 'package:firebase_auth_secure_storage_flutter/firebase_auth_secure_storage_flutter.dart';
import 'package:flutter/widgets.dart';
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
runApp(_Message(await _signIn()));
}
Future<String> _signIn() async {
final FirebaseUserAuth auth = FirebaseUserAuth(
apiKey: 'demo-api-key',
storage: SecureAuthStorage(),
emulatorHost: '127.0.0.1:9099',
);
try {
await auth.restore();
if (auth.uid == null) {
await auth.signInAnonymously();
}
return 'Signed in as ${auth.uid}; the session survives a restart.';
} on SecureAuthStorageException catch (e) {
// No keyring, a locked keychain, a missing entitlement: the message says
// which, and what to do.
return '$e';
} on Exception catch (e) {
// The Auth emulator is not running, for example.
return 'Sign-in failed: $e';
} finally {
await auth.dispose();
}
}
class _Message extends StatelessWidget {
const _Message(this.text);
final String text;
@override
Widget build(BuildContext context) {
debugPrint(text);
return Directionality(
textDirection: TextDirection.ltr,
child: Center(child: Text(text)),
);
}
}