signInWithCustomToken method

Future<UserCredential> signInWithCustomToken(
  1. String token
)

Tries to sign in a user with a given custom token.

Custom tokens are used to integrate Firebase Auth with existing auth systems, and must be generated by the auth backend.

If successful, it also signs the user in into the app and updates any authStateChanges, idTokenChanges stream listeners.

If the user identified by the User.uid specified in the token doesn't have an account already, one will be created automatically.

Read how to use Custom Token authentication and the cases where it is useful in the guides.

A FirebaseAuthException maybe thrown with the following error code:

  • custom-token-mismatch:
  • Thrown if the custom token is for a different Firebase App.
  • invalid-custom-token:
  • Thrown if the custom token format is incorrect.

Implementation

Future<UserCredential> signInWithCustomToken(String token) async {
  final response = await _api.customTokenAuth.signInWithCustomToken(token);

  final user = await _getUserFromIdToken(response.idToken, response);

  _updateCurrentUserAndEvents(user, true);

  return UserCredential._(
    auth: this,
    credential: AuthCredential(
      providerId: ProviderId.custom.signInProvider,
      signInMethod: ProviderId.custom.signInProvider,
    ),
    additionalUserInfo: AdditionalUserInfo(isNewUser: response.isNewUser),
  );
}