signUpWithEmail method

Future<GotrueSessionResponse> signUpWithEmail(
  1. String email,
  2. String password, {
  3. AuthOptions? options,
  4. Map<String, dynamic>? userMetadata,
})

Creates a new user using their email address.

userMetadata sets User.userMetadata without an extra call to updateUser

Implementation

Future<GotrueSessionResponse> signUpWithEmail(
  String email,
  String password, {
  AuthOptions? options,
  Map<String, dynamic>? userMetadata,
}) async {
  try {
    final body = {
      'email': email,
      'password': password,
      'data': userMetadata,
    };
    final fetchOptions = FetchOptions(headers);
    final urlParams = [];
    if (options?.redirectTo != null) {
      final encodedRedirectTo = Uri.encodeComponent(options!.redirectTo!);
      urlParams.add('redirect_to=$encodedRedirectTo');
    }
    final queryString = urlParams.isNotEmpty ? '?${urlParams.join('&')}' : '';
    final response = await _fetch.post(
      '$url/signup$queryString',
      body,
      options: fetchOptions,
    );
    final data = response.rawData as Map<String, dynamic>?;
    if (response.error != null) {
      return GotrueSessionResponse.fromResponse(response: response);
    } else if (data?['access_token'] == null) {
      // email validation required
      User? user;
      if (data?['id'] != null) {
        user = User.fromJson(data!);
      }
      return GotrueSessionResponse.fromResponse(
        response: response,
        user: user,
      );
    } else {
      final session =
          Session.fromJson(response.rawData as Map<String, dynamic>);
      return GotrueSessionResponse.fromResponse(
        response: response,
        data: session,
      );
    }
  } catch (e) {
    return GotrueSessionResponse(error: GotrueError(e.toString()));
  }
}