-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Secure auth store example #145
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5eec158
chore(deps): update `graphql` from 5.1.3 to the latest 5.2.0-beta.9
totzk9 b1b0877
chore(deps): add optional parameters for new features of graphql pacakge
totzk9 292af4c
Merge branch 'main' into main
onehassan de609b2
chore: add flutter_secure_storage on example
totzk9 e4c0db0
chore: create a new example for secure persistent auth store
totzk9 3aa36f3
Merge branch 'nhost:main' into secure_auth_store
totzk9 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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); | ||
} | ||
} |
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
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
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
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the version upgrade to
graphql: ^5.2.0-beta.9
required for the this example ?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No. I messed up the merging of main branch