Firebase Admin SDK 用于从服务器登录用户 [英] Firebase Admin SDK to Log user in from Server

查看:27
本文介绍了Firebase Admin SDK 用于从服务器登录用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在我的 Node JS 代码中使用 Firebase Admin SDK 来验证服务器端的用户.

I have been using Firebase Admin SDK in my Node JS code for authenticating users from the server side.

Admin SDK 提供了一种方法 admin.auth().createUser() 来创建新用户.

The Admin SDK provides a method admin.auth().createUser() to create new users.

admin.auth().createUser({
        email,
        password,
        displayName: name
    })
    .then(function(user) {
        console.log("Successfully created new user:", user.uid)
        return res.send(user)
    })
    .catch(function(err) {
        console.log("Error creating new user:", err)
        return res.send(err)
    })

但是现在如何让用户登录,就像 Firebase Client SDK 中的 auth.signInWithEmailAndPassword(email, pass) 方法一样.

But now how to make a user login like there is a method auth.signInWithEmailAndPassword(email, pass) in the Firebase Client SDK.

firebase admin SDK 中有一个方法可以通过电子邮件 admin.auth().getUserByEmail(email) 获取用户信息.此方法返回包括密码在内的所有用户信息,但该密码是经过哈希处理的.那么现在是否有任何解决方法可以从服务器获得适当的身份验证用户.

There is a method on the firebase admin SDK to get the user info by Email admin.auth().getUserByEmail(email). This method returns all the user information including password but that password is hashed. So now is there any workaround to have a proper authenticate users from Server.

推荐答案

我的评论有点晚了,但一种选择是直接使用 Firebase REST API,但将其集成到您自己的服务器端 API 中进行身份验证.然后使用它和 Admin SDK 的组合来包装它.可以在此处找到 REST API 文档 https://firebase.google.com/docs/reference/rest/auth/#section-sign-in-email-password.您可以让您的客户端保持轻量级,并通过您自己的 API 完成所有 Firebase 身份验证、自定义声明、登录、注销等.您只需要使用这两种方法即可.这会将您从应用程序和 API 中的任何依赖项中抽象出来,因为您可以将它们全部放在一个服务提供者中.仅供参考.

My comment is a bit late but one option would be to use the Firebase REST API directly but integrated into your own server-side API for authentication. And then use a combination of that and the Admin SDK to wrap it all up. REST API docs can be found here https://firebase.google.com/docs/reference/rest/auth/#section-sign-in-email-password. You could keep your client light weight and wrap up all Firebase auth stuff, custom claims, login, logout etc. all through your own API. You would just need to use both methods to do so. This would abstract you away from any dependencies in your app and API as you could put it all in a single service provider. Just food for thought.

这篇关于Firebase Admin SDK 用于从服务器登录用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆