Skip to content

Commit

Permalink
feat(nhost_flutter_auth): Allow displayName and locale to be passed o…
Browse files Browse the repository at this point in the history
…n 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
  • Loading branch information
sl1mpshady authored Feb 6, 2024
1 parent 1e5218f commit 017bcc5
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
31 changes: 27 additions & 4 deletions packages/nhost_auth_dart/lib/src/auth_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> signInAnonymous() async {
Future<void> signInAnonymous(
String? displayName,
String? locale,
Map<String, dynamic>? 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].
Expand Down
6 changes: 5 additions & 1 deletion packages/nhost_sdk/lib/src/base/hasura_auth_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ abstract class HasuraAuthClient {
String? redirectTo,
});

Future<void> signInAnonymous();
Future<void> signInAnonymous(
String? displayName,
String? locale,
Map<String, dynamic>? metadata,
);
Future<void> signInWithSmsPasswordless({
required String phoneNumber,
String? locale,
Expand Down

0 comments on commit 017bcc5

Please sign in to comment.