Skip to content

Commit 8fd99cd

Browse files
authored
feat: add getUser() method (supabase#761)
2 parents ccb2f5d + 0b0073e commit 8fd99cd

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

packages/gotrue/lib/src/gotrue_client.dart

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,23 @@ class GoTrueClient {
596596
}
597597
}
598598

599+
/// Gets the current user details from current session or custom [jwt]
600+
Future<UserResponse> getUser([String? jwt]) async {
601+
if (jwt == null && currentSession?.accessToken == null) {
602+
throw AuthException('Cannot get user: no current session.');
603+
}
604+
final options = GotrueRequestOptions(
605+
headers: _headers,
606+
jwt: jwt ?? currentSession?.accessToken,
607+
);
608+
final response = await _fetch.request(
609+
'$_url/user',
610+
RequestMethodType.get,
611+
options: options,
612+
);
613+
return UserResponse.fromJson(response);
614+
}
615+
599616
/// Updates user data, if there is a logged in user.
600617
Future<UserResponse> updateUser(
601618
UserAttributes attributes, {
@@ -679,12 +696,7 @@ class GoTrueClient {
679696
throw AuthException('No token_type detected.');
680697
}
681698

682-
final headers = {..._headers};
683-
headers['Authorization'] = 'Bearer $accessToken';
684-
final options = GotrueRequestOptions(headers: headers);
685-
final response = await _fetch.request('$_url/user', RequestMethodType.get,
686-
options: options);
687-
final user = UserResponse.fromJson(response).user;
699+
final user = (await getUser(accessToken)).user;
688700
if (user == null) {
689701
throw AuthException('No user found.');
690702
}

0 commit comments

Comments
 (0)