Skip to content

Commit

Permalink
chore: create a new example for secure persistent auth store
Browse files Browse the repository at this point in the history
  • Loading branch information
totzk9 committed Dec 4, 2024
1 parent de609b2 commit e4c0db0
Showing 1 changed file with 104 additions and 0 deletions.
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);
}
}

0 comments on commit e4c0db0

Please sign in to comment.