Home > front end >  Firebase Auth: How to sign in using id token?
Firebase Auth: How to sign in using id token?

Time:01-10

Is there a way to share anonymous user sessions with Firebase Auth?

What I would like to is to get the current id token:

final idToken = FirebaseAuth.instance.currentUser!.getIdToken();

Then, use this idToken to authenticate the same anonymous user in a different app.

FirebaseAuth.instance.signInWithToken(idToken);

CodePudding user response:

With signInWithCustomToken() method, you can use a custom auth token to sign in a user on different website. As documented here

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.

You can create a custom token with the Firebase Admin SDK, or you can use a third-party JWT library if your server is written in a language which Firebase does not natively support.

The Firebase Admin SDK has a built-in method for creating custom tokens. At a minimum, you need to provide a uid, which can be any string but should uniquely identify the user or device you are authenticating. These tokens expire after one hour.

After you create a custom token, you should send it to your client app. The client app authenticates with the custom token by calling signInWithCustomToken()

Also check out these links for more information and examples:

Authenticate with Firebase Using a Custom Authentication System

Firebase auth - login user from app in website

How to use the same firebase anonymous user in a flutter app

  • Related