简体   繁体   English

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

[英]Sign in with Firebase-Admin using node.js

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 . 我正在尝试使用firebase-admin登录node.js,但是当我查找API时,他们只有updatedeletecreate

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. 我觉得我错误地阅读了如何使用firebase-admin。 My best guess is that I should be using straight Firebase and not the new firebase-admin. 我最好的猜测是我应该使用直接的Firebase而不是新的firebase-admin。

Edit: 编辑:

I only want to sign in the user by email (ie not by google sign in or facebook login), if that is possible. 我只想通过电子邮件登录用户(即不是通过谷歌登录或Facebook登录),如果可能的话。

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. Firebase Admin Node.js SDKfirebase-admin on npm)用于管理操作,例如获取用户数据或更改用户的电子邮件而不使用现有密码。 If you just want to sign in as a user, you should use the Firebase client Node.js SDK ( firebase on npm). 如果你只是想登录的用户,你应该使用火力地堡客户端的Node.js SDKfirebase上NPM)。

Here is the answer in another post: How to authenticate an user in firebase-admin in nodejs? 以下是另一篇文章的答案: 如何在nodejs中对firebase-admin中的用户进行身份验证? .

Copy and Paste, just in case: 复制和粘贴,以防万一:

Install firebase module: npm install firebase --save 安装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'));
    }
});

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 注册: https//www.googleapis.com/identitytoolkit/v3/relyingparty/signupNewUser

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

This allows to create/signin users by firing a http request to the above urls. 这允许通过向上述URL发起http请求来创建/签名用户。 You get the required tokens and these are compatible with firebase. 您将获得所需的令牌,这些令牌与firebase兼容。 I assume firebase internally uses these url's in the client libraries. 我假设firebase内部在客户端库中使用这些url。

Hope its helpful! 希望它有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Node.js 和“firebase-admin”SDK 查询 Firebase? - How to query Firebase with Node.js and "firebase-admin" SDK? 如何在firebase-admin node.js中限制.once('value) - How to limit .once('value) in firebase-admin node.js 无法在 Node.js 中导入 firebase-admin - Can't import firebase-admin in Node.js Node.js 12.x AWS lambda 不使用 firebase-admin 实时数据库返回 - Node.js 12.x AWS lambda does not return using firebase-admin realtime database 如何使用firebase-admin模块使用Node.js从Firebase数据库返回有序数据? - How can I return ordered data from Firebase database with Node.js using the firebase-admin module? firebase JS SDK 包是否与云函数的 Node.js 环境兼容? 为什么要使用 firebase-admin? - Is the firebase JS SDK package compatible with the Node.js environment of a cloud function? Why use firebase-admin? Firebase-admin Firestore node.js 与模块化 v9 js sdk 共享逻辑 - Firebase-admin Firestore node.js share logic with modular v9 js sdk 为node.js安装firebase-admin时出现grpc安装错误 - grpc installion error while installing firebase-admin for node.js 在 Node 中使用 firebase-admin 将火上传到 Firebase 存储 - Uploading fire to Firebase Storage using firebase-admin in Node 在 netlify 函数中使用 firebase-admin - using firebase-admin in netlify function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM