Assuming that I have a whole user object as a JSON (with its access and refresh tokens) – how can I force firebase/auth
to accept it as its currentUser
so it triggers e.g. onTokenIdChange
?
EDIT:
I want to authenticate the user on a website (my app #1), and inform the browser extension (my app #2) if successful. Right now I’m passing the JSON representation of a user returned from the website’s onTokenIdChange
to the extension.
EDIT2:
This is the user’s JSON I’m referring to:
{
"uid": "some-uid-here",
"email": "[email protected]",
"emailVerified": false,
"isAnonymous": false,
"providerData": [
{
"providerId": "password",
"uid": "[email protected]",
"displayName": null,
"email": "[email protected]",
"phoneNumber": null,
"photoURL": null
}
],
"stsTokenManager": {
"refreshToken": "my-refresh-token",
"accessToken": "my-access-token",
"expirationTime": 1664750677967
},
"createdAt": "1664712317951",
"lastLoginAt": "1664740255685",
"apiKey": "my-api-key",
"appName": "[DEFAULT]"
}
2
Answers
Passing the user profile around is not enough to authenticate a user with. If you want to authenticate the user in a different context, you’ll have to pass their credentials around or mint a custom token for them and sign them in with that.
Not sure if I understood the question, but as far as I know, you have to either
Use one of the default signIn methods (like
signInWithEmailAndPassword
), which requires user input; I think you are chasing something differentUse the custom token approach mentioned by Frank. In that case, your backend application will use the
firebase-admin-sdk
to generate custom tokens for a given user, acording to the business logic you desire. Only after that your frontend application will use thefirebase/auth
lib to run thesignInWithCustomToken
, which will log you in.Example: your app #2 must have a way to communicate with your backend in order to retrieve a custom token for the given user