From 77010bff934f93d234ddce6f9353e3aba16cd356 Mon Sep 17 00:00:00 2001 From: sl1mpshady Date: Fri, 26 Jan 2024 22:14:11 +0400 Subject: [PATCH 1/2] feat(nhost_flutter_auth): Allow displayName and locale to be passed on signInAnonymous --- packages/nhost_auth_dart/lib/src/auth_client.dart | 9 ++++++++- packages/nhost_sdk/lib/src/base/hasura_auth_client.dart | 5 ++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/packages/nhost_auth_dart/lib/src/auth_client.dart b/packages/nhost_auth_dart/lib/src/auth_client.dart index 18866975..ddfe0279 100644 --- a/packages/nhost_auth_dart/lib/src/auth_client.dart +++ b/packages/nhost_auth_dart/lib/src/auth_client.dart @@ -295,10 +295,17 @@ class NhostAuthClient implements HasuraAuthClient { /// Nhost dashboard -> settings -> Sign in methods -> Anonymous Users /// Throws an [NhostException] if sign in fails. @override - Future signInAnonymous() async { + Future signInAnonymous( + String? displayName, + String? locale, + ) async { log.finer('Attempting sign in anonymously'); await _apiClient.post( '/signin/anonymous', + jsonBody: { + if (displayName != null) 'displayName': displayName, + if (locale != null) 'locale': locale, + }, ); } diff --git a/packages/nhost_sdk/lib/src/base/hasura_auth_client.dart b/packages/nhost_sdk/lib/src/base/hasura_auth_client.dart index 9a609c04..9c94aa6b 100644 --- a/packages/nhost_sdk/lib/src/base/hasura_auth_client.dart +++ b/packages/nhost_sdk/lib/src/base/hasura_auth_client.dart @@ -43,7 +43,10 @@ abstract class HasuraAuthClient { String? redirectTo, }); - Future signInAnonymous(); + Future signInAnonymous( + String? displayName, + String? locale, + ); Future signInWithSmsPasswordless({ required String phoneNumber, String? locale, From 572ebdf97b78992c851dd08f67d092e76ed843f5 Mon Sep 17 00:00:00 2001 From: sl1mpshady Date: Mon, 5 Feb 2024 16:21:13 +0400 Subject: [PATCH 2/2] feat(nhost_flutter_auth): Add metadata to the request body and set session after signing in anonymously --- .../nhost_auth_dart/lib/src/auth_client.dart | 30 ++++++++++++++----- .../lib/src/base/hasura_auth_client.dart | 1 + 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/packages/nhost_auth_dart/lib/src/auth_client.dart b/packages/nhost_auth_dart/lib/src/auth_client.dart index ddfe0279..4fd6bd0f 100644 --- a/packages/nhost_auth_dart/lib/src/auth_client.dart +++ b/packages/nhost_auth_dart/lib/src/auth_client.dart @@ -298,15 +298,31 @@ class NhostAuthClient implements HasuraAuthClient { Future signInAnonymous( String? displayName, String? locale, + Map? metadata, ) async { log.finer('Attempting sign in anonymously'); - await _apiClient.post( - '/signin/anonymous', - jsonBody: { - if (displayName != null) 'displayName': displayName, - if (locale != null) 'locale': locale, - }, - ); + + AuthResponse? res; + try { + res = await _apiClient.post( + '/signin/anonymous', + jsonBody: { + if (displayName != null) 'displayName': displayName, + if (locale != null) 'locale': locale, + if (metadata != null) 'metadata': metadata + }, + responseDeserializer: AuthResponse.fromJson, + ); + } catch (e, st) { + log.finer('Sign in anonymously failed', e, st); + await clearSession(); + rethrow; + } + + if (res != null) { + log.finer('Sign in anonymously successful'); + await setSession(res.session!); + } } /// Authenticates a user using a [phoneNumber]. diff --git a/packages/nhost_sdk/lib/src/base/hasura_auth_client.dart b/packages/nhost_sdk/lib/src/base/hasura_auth_client.dart index 9c94aa6b..08737f75 100644 --- a/packages/nhost_sdk/lib/src/base/hasura_auth_client.dart +++ b/packages/nhost_sdk/lib/src/base/hasura_auth_client.dart @@ -46,6 +46,7 @@ abstract class HasuraAuthClient { Future signInAnonymous( String? displayName, String? locale, + Map? metadata, ); Future signInWithSmsPasswordless({ required String phoneNumber,