As the title implies, how to generate access token from username and password on the server?
There are methods to that on the client, but I can't find anything to do that directly on serve side.
As the title implies, how to generate access token from username and password on the server?
There are methods to that on the client, but I can't find anything to do that directly on serve side.
Using the Firebase Admin SDK to create custom tokens based on sign-in credentials is confusing, since the documentation states:
Create Custom Token
Firebase gives you complete control over authentication by allowing you to authenticate users or devices using secure JSON Web Tokens (JWTs). You generate these tokens on your server, pass them back to a client device, and then use them to authenticate via the
signInWithCustomToken()
method.To achieve this, you must create a server endpoint that accepts sign-in credentials—such as a username and password—and, if the credentials are valid, returns a custom JWT. The custom JWT returned from your server can then be used by a client device to authenticate with Firebase (iOS, Android, web).
There is an example of creating a custom token based on the user's Firebase uid
:
admin.auth().createCustomToken(uid)
But the documentation does not show how to verify if user credentials are valid in the first place.
Since the Firebase Admin SDK does not provide methods for verifying credentials, a work around is to use a combination of the Firebase Admin SDK as well as the Firebase Auth REST API.
getUserByEmail
UserRecord
will contain the uid
, which can then be passed to admin.auth().createCustomToken(uid)
From what i see, you can use Firebase Flutter package to login with Username and Password.
var credentials = await auth.signInWithEmailAndPassword(email: email, password: password);
String token = await credentials.user.getIdToken();
Then you can pass this token in the HTTP header as Bearer
token
In the server side, you can just use verifyToken
from Admin SDK
FirebaseAuth.getInstance().verifyIdToken(token)