使用node.js使用Firebase-Admin登录

时间:2017-03-22 23:41:22

标签: node.js firebase firebase-authentication firebase-admin

我正在尝试使用firebase-admin登录node.js,但是当我查找API时,他们只有updatedeletecreate的部分。

他们确实有关于如何通过电子邮件获取用户的部分,但如果我想登录用户,我也不应该通过他们的密码进行验证。我觉得我错误地阅读了如何使用firebase-admin。我最好的猜测是我应该使用直接的Firebase而不是新的firebase-admin。

编辑:

我只想通过电子邮件登录用户(即不是通过谷歌登录或登录Facebook),如果可能的话。

3 个答案:

答案 0 :(得分:3)

Firebase Admin Node.js SDKfirebase-admin on npm)用于管理操作,例如在没有现有密码的情况下获取用户数据或更改用户的电子邮件。如果您只想以用户身份登录,则应使用Firebase client Node.js SDKfirebase npm)。

答案 1 :(得分:0)

这是另一篇文章的答案: How to authenticate an user in firebase-admin in nodejs?

复制并粘贴,以防万一:

安装firebase模块: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 :(得分:-1)

如果您仍在寻找不使用客户端库的登录方式,则是这种方法。

根据所需操作向以下网址发送请求

注册:https://www.googleapis.com/identitytoolkit/v3/relyingparty/signupNewUser

登录:https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyPassword

这允许通过向上述网址触发http请求来创建/登录用户。您将获得所需的令牌,并且这些令牌与Firebase兼容。我假设firebase在客户端库中内部使用这些url。

希望它有帮助!