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 #145

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,17 @@

#include "generated_plugin_registrant.h"

#include <flutter_secure_storage_linux/flutter_secure_storage_linux_plugin.h>
#include <gtk/gtk_plugin.h>
#include <url_launcher_linux/url_launcher_plugin.h>

void fl_register_plugins(FlPluginRegistry* registry) {
g_autoptr(FlPluginRegistrar) flutter_secure_storage_linux_registrar =
fl_plugin_registry_get_registrar_for_plugin(registry, "FlutterSecureStorageLinuxPlugin");
flutter_secure_storage_linux_plugin_register_with_registrar(flutter_secure_storage_linux_registrar);
g_autoptr(FlPluginRegistrar) gtk_registrar =
fl_plugin_registry_get_registrar_for_plugin(registry, "GtkPlugin");
gtk_plugin_register_with_registrar(gtk_registrar);
g_autoptr(FlPluginRegistrar) url_launcher_linux_registrar =
fl_plugin_registry_get_registrar_for_plugin(registry, "UrlLauncherPlugin");
url_launcher_plugin_register_with_registrar(url_launcher_linux_registrar);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#

list(APPEND FLUTTER_PLUGIN_LIST
flutter_secure_storage_linux
gtk
url_launcher_linux
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@ import FlutterMacOS
import Foundation

import app_links
import flutter_secure_storage_macos
import path_provider_foundation
import shared_preferences_foundation
import url_launcher_macos

func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
AppLinksMacosPlugin.register(with: registry.registrar(forPlugin: "AppLinksMacosPlugin"))
FlutterSecureStoragePlugin.register(with: registry.registrar(forPlugin: "FlutterSecureStoragePlugin"))
PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin"))
SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin"))
UrlLauncherPlugin.register(with: registry.registrar(forPlugin: "UrlLauncherPlugin"))
}
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@
#include "generated_plugin_registrant.h"

#include <app_links/app_links_plugin_c_api.h>
#include <flutter_secure_storage_windows/flutter_secure_storage_windows_plugin.h>
#include <url_launcher_windows/url_launcher_windows.h>

void RegisterPlugins(flutter::PluginRegistry* registry) {
AppLinksPluginCApiRegisterWithRegistrar(
registry->GetRegistrarForPlugin("AppLinksPluginCApi"));
FlutterSecureStorageWindowsPluginRegisterWithRegistrar(
registry->GetRegistrarForPlugin("FlutterSecureStorageWindowsPlugin"));
UrlLauncherWindowsRegisterWithRegistrar(
registry->GetRegistrarForPlugin("UrlLauncherWindows"));
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

list(APPEND FLUTTER_PLUGIN_LIST
app_links
flutter_secure_storage_windows
url_launcher_windows
)

Expand Down