[英]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时,他们只有
update
, delete
和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.
我觉得我错误地阅读了如何使用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 SDK (
firebase-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 SDK (
firebase
上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.