-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: create a new example for secure persistent auth store
- Loading branch information
Showing
1 changed file
with
104 additions
and
0 deletions.
There are no files selected for viewing
104 changes: 104 additions & 0 deletions
104
packages/nhost_flutter_auth/example/lib/secure_persistent_auth_example.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/// This example demonstrates the ability to store authentication tokens between | ||
/// restarts of your application, so that the user is logged in automatically. | ||
/// | ||
/// This example depends on the `flutter_secure_storage` package, which is used to | ||
/// implement the [SecureAuthStore] class. | ||
/// | ||
/// To try it out, run the application, log in, then restart the app. You should | ||
/// see the contents of [ExampleProtectedScreen] without having to log in a | ||
/// second time. | ||
library secure_persistent_auth_example; | ||
|
||
import 'dart:async'; | ||
|
||
import 'package:flutter/material.dart'; | ||
import 'package:nhost_flutter_auth/nhost_flutter_auth.dart'; | ||
import 'package:flutter_secure_storage/flutter_secure_storage.dart'; | ||
|
||
import 'config.dart'; | ||
import 'simple_auth_example.dart'; | ||
|
||
void main() { | ||
runApp(const SecurePersistentAuthExample()); | ||
} | ||
|
||
class SecurePersistentAuthExample extends StatefulWidget { | ||
const SecurePersistentAuthExample({ | ||
super.key, | ||
}); | ||
|
||
@override | ||
SecurePersistentAuthExampleState createState() => SecurePersistentAuthExampleState(); | ||
} | ||
|
||
class SecurePersistentAuthExampleState extends State<SecurePersistentAuthExample> { | ||
late NhostClient nhostClient; | ||
|
||
@override | ||
void initState() { | ||
super.initState(); | ||
// Create a new Nhost client using your project's subdomain and region. | ||
nhostClient = NhostClient( | ||
subdomain: Subdomain( | ||
subdomain: subdomain, | ||
region: region, | ||
), | ||
// Instruct the client to store tokens using shared preferences. | ||
authStore: const SecureAuthStore(), | ||
); | ||
// this will fetch refresh token and will sign user in! | ||
nhostClient.auth | ||
.signInWithStoredCredentials() | ||
.then((value) => null) | ||
.catchError( | ||
(e) { | ||
// ignore: avoid_print | ||
print(e); | ||
}, | ||
); | ||
} | ||
|
||
@override | ||
void dispose() { | ||
super.dispose(); | ||
nhostClient.close(); | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return NhostAuthProvider( | ||
auth: nhostClient.auth, | ||
child: const MaterialApp( | ||
title: 'Nhost.io Persistent Flutter Authentication Example', | ||
home: Scaffold( | ||
body: ExampleProtectedScreen(), | ||
), | ||
), | ||
); | ||
} | ||
} | ||
|
||
/// An Nhost [AuthStore] implementation backed by the `flutter_secure_storage` | ||
/// plugin, so authentication information is retained between runs of the | ||
/// application. | ||
class SecureAuthStore implements AuthStore { | ||
const SecureAuthStore(); | ||
|
||
@override | ||
Future<String?> getString(String key) { | ||
const FlutterSecureStorage storage = FlutterSecureStorage(); | ||
return storage.read(key: key); | ||
} | ||
|
||
@override | ||
Future<void> setString(String key, String value) { | ||
const FlutterSecureStorage storage = FlutterSecureStorage(); | ||
return storage.write(key: key, value: value); | ||
} | ||
|
||
@override | ||
Future<void> removeItem(String key) { | ||
const FlutterSecureStorage storage = FlutterSecureStorage(); | ||
return storage.delete(key: key); | ||
} | ||
} |