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?
Solution 1:
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.
Solution 2:
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:
- Prompt the username to login with username and password
- Verify the username exists in your database and retrieve the corresponding email address for that account
- Pass this email address to the login process seamlessly
Solution 3:
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 :
User send username and password to cloud function url via query params (GET) or request body (POST)
Cloud funtion will check whether username and password is valid (ex: from realtime database)
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
Client then can login using that customToken