From 017bcc575b2a6149d1817656913f351af9c58090 Mon Sep 17 00:00:00 2001 From: "Siegfred B. Pagador" Date: Tue, 6 Feb 2024 14:15:21 +0400 Subject: [PATCH] feat(nhost_flutter_auth): Allow displayName and locale to be passed on signInAnonymous (#131) * feat(nhost_flutter_auth): Allow displayName and locale to be passed on signInAnonymous * 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 | 31 ++++++++++++++++--- .../lib/src/base/hasura_auth_client.dart | 6 +++- 2 files changed, 32 insertions(+), 5 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..4fd6bd0f 100644 --- a/packages/nhost_auth_dart/lib/src/auth_client.dart +++ b/packages/nhost_auth_dart/lib/src/auth_client.dart @@ -295,11 +295,34 @@ 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, + Map? metadata, + ) async { log.finer('Attempting sign in anonymously'); - await _apiClient.post( - '/signin/anonymous', - ); + + 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 9a609c04..08737f75 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,11 @@ abstract class HasuraAuthClient { String? redirectTo, }); - Future signInAnonymous(); + Future signInAnonymous( + String? displayName, + String? locale, + Map? metadata, + ); Future signInWithSmsPasswordless({ required String phoneNumber, String? locale,