9

I am trying to sign in with node.js with firebase-admin, but when I look up there API, they only have sections on update, delete and create.

They do have sections on how to get the user by email but if i want to sign in a user should I not be verifying also by their password as well. I feel like I am incorrectly reading how to use firebase-admin. My best guess is that I should be using straight Firebase and not the new firebase-admin.

Edit:

I only want to sign in the user by email (i.e. not by google sign in or facebook login), if that is possible.

AL.
  • 36,815
  • 10
  • 142
  • 281
Matthew
  • 800
  • 3
  • 12
  • 35
  • You can use the client side node.js Firebase library for that: https://www.npmjs.com/package/firebase – bojeil Mar 23 '17 at 01:41

3 Answers3

16

The Firebase Admin Node.js SDK (firebase-admin on npm) is for administrative actions like fetching user data or changing a user's email without their existing password. If you just want to sign in as a user, you should use the Firebase client Node.js SDK (firebase on npm).

jwngr
  • 4,284
  • 1
  • 24
  • 27
  • it turns out that I was not properly configuring `firebase` and that was my problem. – Matthew Mar 23 '17 at 03:00
  • 1
    This is probably what firebase meant for but this approach what makes firebase hard to understand (at least for me) – chenop May 01 '20 at 16:18
1

Here is the answer in another post: How to authenticate an user in firebase-admin in nodejs?.

Copy and Paste, just in case:

Install firebase module: npm install firebase --save

const firebase = require("firebase");
const config = {
    apiKey: "",
    authDomain: "",
    databaseURL: "",
    projectId: "",
    storageBucket: "",
    messagingSenderId: ""
};
firebase.initializeApp(config);
exports.login = functions.https.onRequest((req, rsp)=>{
    const email = req.body.email;
    const password = req.body.password;
    const key = req.body.key;
    const _key = '_my_key_';
    let token = '';
    if(key === _key){           
    firebase.auth().signInWithEmailAndPassword(email,password).then((user)=>{
//The promise sends me a user object, now I get the token, and refresh it by sending true (obviously another promise)            
user.getIdToken(true).then((token)=>{
                rsp.writeHead(200, {"Content-Type": "application/json"});
                rsp.end(JSON.stringify({token:token}));
            }).catch((err)=>{
                rsp.writeHead(500, {"Content-Type": "application/json"});
                rsp.end(JSON.stringify({error:err}));
            });
        }).catch((err)=>{
            rsp.writeHead(500, {"Content-Type": "application/json"});
            rsp.end(JSON.stringify({error:err}));
        });
    } else {
        rsp.writeHead(500, {"Content-Type": "application/json"});
        rsp.end(JSON.stringify('error - no key'));
    }
});
-2

If you are still looking for sign in without using client libraries, here is the approach.

Fire a request to the following url's based on the required action

Signup : https://www.googleapis.com/identitytoolkit/v3/relyingparty/signupNewUser

Signin : https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyPassword

This allows to create/signin users by firing a http request to the above urls. You get the required tokens and these are compatible with firebase. I assume firebase internally uses these url's in the client libraries.

Hope its helpful!

Ayyappa
  • 1,876
  • 1
  • 21
  • 41