Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to login as another user in Firebase?

I'm working on an application where I'll need to help users with certain tasks as part of my customer service. Rather than build a separate admin interface, I'd prefer to have the ability to impersonate users to use the app for them.

Is this something that Firebase can do?

like image 994
leros Avatar asked Dec 07 '18 21:12

leros


2 Answers

Yes!

  1. Using admin SDK and service account initialize Firebase app backend with:
admin.initializeApp({
  credential: admin.credential.cert(service_account_json),
})
  1. Obtain authentication token for the user you wish to impersonate:
const userId = "[impersonating user uid string]"
const token = await admin.auth().createCustomToken(userId)
  1. Using frontend Firebase SDK authenticate user with:
const token = "[token string obtained in step 2]"
firebase.auth().signInWithCustomToken(token)

Done! You're now impersonating selected user.

For obvious security reasons the backend endpoint, like Google Cloud Function should require authentication and verify if user requesting custom token is actually a privileged user (admin), to avoid situation where any authenticated user is able to impersonate anyone.

like image 125
mal Avatar answered Nov 12 '22 02:11

mal


The Firebase Authentication client-side SDKs have no built-in impersonation mechanism. To sign in as a specific user on these SDKs, you must know that user's credentials.

The Firebase Admin SDKs supports impersonating a user in its interaction with the Realtime Database. Since the Admin SDK is meant to be run in a trusted environment (such as your development machine, a server you control, or Cloud Functions) they run with administrative privileges. To learn more about impersonating a regular user here, see the documentation on authenticating with limited privileges.

like image 23
Frank van Puffelen Avatar answered Nov 12 '22 00:11

Frank van Puffelen