Firebase Admin SDK to Log user in from Server
Asked Answered
S

3

8

I have been using Firebase Admin SDK in my Node JS code for authenticating users from the server side.

The Admin SDK provides a method admin.auth().createUser() to create new users.

admin.auth().createUser({
        email,
        password,
        displayName: name
    })
    .then(function(user) {
        console.log("Successfully created new user:", user.uid)
        return res.send(user)
    })
    .catch(function(err) {
        console.log("Error creating new user:", err)
        return res.send(err)
    })

But now how to make a user login like there is a method auth.signInWithEmailAndPassword(email, pass) in the Firebase Client SDK.

There is a method on the firebase admin SDK to get the user info by Email admin.auth().getUserByEmail(email). This method returns all the user information including password but that password is hashed. So now is there any workaround to have a proper authenticate users from Server.

Silique answered 13/8, 2018 at 6:0 Comment(1)
See also: #59203316Geniality
C
15

My comment is a bit late but one option would be to use the Firebase REST API directly but integrated into your own server-side API for authentication. And then use a combination of that and the Admin SDK to wrap it all up. REST API docs can be found here https://firebase.google.com/docs/reference/rest/auth/#section-sign-in-email-password. You could keep your client light weight and wrap up all Firebase auth stuff, custom claims, login, logout etc. all through your own API. You would just need to use both methods to do so. This would abstract you away from any dependencies in your app and API as you could put it all in a single service provider. Just food for thought.

Crackerjack answered 12/4, 2019 at 15:43 Comment(2)
Anyone know if this is possible/recommended within firebase cloud functions?Angeli
Possible yes, recommended depends on your usecase. I used it yesterday to transform a backend generated custom token into a firebase ID token. It works fine, just keep in mind you'll need to generate a web API key.Chemush
M
9

There is no way to log a user in with the Admin SDK. The Admin SDK runs with administrative privileges and has no need to log in.

You'll want to use one of the Firebase client-side SDKs (e.g. for Android, iOS or web users) to sign your users in to Firebase directly from the client-side code.

If you want your server-side code to know what user is signed in, you send the token from the client to your server and then decode and validate it there. See the document on verifying an ID token and the sample of server-side authentication with Cloud Functions.

Marsala answered 13/8, 2018 at 6:7 Comment(2)
This is not an ideal solution for those of us who would like to reduce the complexity on our client.Langobard
According to bundlephobia.com the firebase library is 214kb zipped. Thats a huge import if all you want to do is login. I'm really disappointed that this is not possible on the server side.Cherish
V
0

Hi you can use for example in typescript the client sdk in your server side code

import firebase from "firebase/compat/app";

const firebaseConfig = {
  apiKey: "",
  authDomain: "",
  projectId: "",
  storageBucket: "",
  messagingSenderId: "",
  appId: ""
};

firebase.initializeApp(firebaseConfig);
export  const authSdk = firebase.auth()

// now you can do 
authSdk.signInWithEmailAndPassword(userCredential.email, userCredential.password)
.then(loggedUser => {console.log(loggedUser.user?.uid)})
Vinegarroon answered 19/9, 2021 at 0:50 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Formulate

© 2022 - 2024 — McMap. All rights reserved.