Skip to content
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 #154

Merged
merged 3 commits into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 the `flutter_secure_storage`.
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 Secure 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);
}
}
1 change: 1 addition & 0 deletions packages/nhost_flutter_auth/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ dependencies:
provider: '>=6.0.4'
shared_preferences: ^2.0.15
url_launcher: ^6.1.7
flutter_secure_storage: ^9.2.2

# Nhost packages
nhost_flutter_auth:
Expand Down