updateUser method

Future<UserResponse> updateUser(
  1. UserAttributes attributes, {
  2. String? emailRedirectTo,
})

Updates user data, if there is a logged in user.

Implementation

Future<UserResponse> updateUser(
  UserAttributes attributes, {
  String? emailRedirectTo,
}) async {
  final accessToken = currentSession?.accessToken;
  if (accessToken == null) {
    throw AuthSessionMissingException();
  }

  final codeChallenge = attributes.email != null
      ? await _generatePKCECodeChallenge()
      : null;

  final body = {
    ...attributes.toJson(),
    if (attributes.email != null) ...{
      'code_challenge': codeChallenge,
      'code_challenge_method': codeChallenge != null ? 's256' : null,
    },
  };
  final options = GotrueRequestOptions(
    headers: _headers,
    body: body,
    jwt: accessToken,
    redirectTo: emailRedirectTo,
  );
  final response = await _fetch.request(
    '$_url/user',
    RequestMethodType.put,
    options: options,
  );
  final userResponse = UserResponse.fromJson(response);

  _currentSession = currentSession?.copyWith(user: userResponse.user);
  notifyAllSubscribers(AuthChangeEvent.userUpdated);

  return userResponse;
}