signInWithPassword method

Future<AuthResponse> signInWithPassword({
  1. String? email,
  2. String? phone,
  3. required String password,
  4. String? captchaToken,
})

Log in an existing user with an email and password or phone and password.

Implementation

Future<AuthResponse> signInWithPassword({
  String? email,
  String? phone,
  required String password,
  String? captchaToken,
}) async {
  late final Map<String, dynamic> response;

  if (email != null) {
    response = await _fetch.request(
      '$_url/token',
      RequestMethodType.post,
      options: GotrueRequestOptions(
        headers: _headers,
        body: {
          'email': email,
          'password': password,
          'gotrue_meta_security': {'captcha_token': captchaToken},
        },
        query: {'grant_type': 'password'},
      ),
    );
  } else if (phone != null) {
    response = await _fetch.request(
      '$_url/token',
      RequestMethodType.post,
      options: GotrueRequestOptions(
        headers: _headers,
        body: {
          'phone': phone,
          'password': password,
          'gotrue_meta_security': {'captcha_token': captchaToken},
        },
        query: {'grant_type': 'password'},
      ),
    );
  } else {
    throw AuthException(
      'You must provide either an email, phone number, a third-party provider or OpenID Connect.',
    );
  }

  final authResponse = AuthResponse.fromJson(response);

  if (authResponse.session?.accessToken != null) {
    _saveSession(authResponse.session!);
    notifyAllSubscribers(AuthChangeEvent.signedIn);
  }
  return authResponse;
}