vikzilla
vikzilla

Reputation: 4138

Username authentication instead of email

With Firebase I can sign up and login users using email addresses. However what if I want the app to be username based. For example, you would log in with "Bobzilla" instead of "[email protected]"?

Is this possible with Firebase?

Upvotes: 26

Views: 22403

Answers (4)

Frank van Puffelen
Frank van Puffelen

Reputation: 598857

There is no default username+password provider built into Firebase Authentication. But you can create your own custom identity provider using the instructions in the Firebase documentation. This requires code that runs in a trusted environment, for which you can use your own server or Cloud Functions for Firebase. There is now even an example of this in the functions-samples repo.


Alternatively: you can use the built-in email+password provider and simply add any domain behind the username. So once you have determined the user name, register your user with <username>@vikzillasapp.com.

Note that this will make it impossible to for the user to reset their password if they forget it, since Firebase uses the email address to send the password reset email.

Upvotes: 31

Morad Abed
Morad Abed

Reputation: 35

For future seekers; As Frank van Puffelen suggested

build an 'email' using the username of the user, such as: [email protected] this will be the email for Auth, but in order to send a reset password email you can use the firebase functions and using the Admin SDK you can send an email.

  1. build the Auth email [email protected]
  2. Store the real - correct email address in userclaims or db.
  3. Using firebase functions and Admin SDK, you can send the password reset to email address you want (After extracting it from the userclaims of the user object).

Upvotes: 1

Scott D
Scott D

Reputation: 1424

Instead of using a method where you assign an email address for the user, it might be a better option to lookup an email address in your database.

An example would be:

  1. Prompt the username to login with username and password
  2. Verify the username exists in your database and retrieve the corresponding email address for that account
  3. Pass this email address to the login process seamlessly

Upvotes: 6

Faruk
Faruk

Reputation: 5821

You can use firebase custom auth. Custom auth is a method which user can login into app using custom token.

But, you need backend code, to create custom token when user send username and password to that backend.

Fortunately, now there is firebase cloud function, with http event, that can solve your problem easily. The step is :

  1. User send username and password to cloud function url via query params (GET) or request body (POST)

  2. Cloud funtion will check whether username and password is valid (ex: from realtime database)

  3. If the username and password valid, cloud function will create custom token using userId (you need to save the userId). And then send it to response body

  4. Client then can login using that customToken

Upvotes: 5

Related Questions