首页 文章

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

提问于
浏览
3

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

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

编辑:

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

2 回答

  • 3

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

  • 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'));
        }
    });
    

相关问题