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,