3
votes

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.

2

2 Answers

7
votes

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.

3
votes

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.