Firebase,如何在node.js RESTapi后端上使用身份验证服务 [英] Firebase, how to use auth service with a node.js RESTapi backend

查看:119
本文介绍了Firebase,如何在node.js RESTapi后端上使用身份验证服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名全栈Java脚本开发人员.

I'm a full-stack java script developer.

我有一个项目,前端是使用vue.js开发的.

I have a project, the front-end was developed using the vue.js.

后端正在使用node.js表达框架进行开发.我的后端也是RESTapi.

Back-end is developing using node.js express framework. My back-end is a RESTapi as well.

自从我的前端UI设计完成后,我创建了一个基于火的项目并将其托管.托管前端部分非常简单.

Since my front-end UI design is finished, I created a fire-base project and hosted it. It was dead simple to host the front-end part.

我的后端还没有身份验证部分.我想在此项目中尝试基于Fire的身份验证服务.

My back-end doesn't have a authentication part yet. I want to try the fire-base auth service in this project.

我进行身份验证的正常方式是这样的.

Normal way how I would do authentication is like this.

  1. 将详细信息传递到后端以创建用户帐户.
  2. 后端创建一个用户帐户.
  3. 然后,当用户尝试记录凭据时,将其传递到后端并从后端释放令牌
  4. 该令牌在用户的浏览器中存储了临时内存,并随他传递给后端的每个请求都附有该令牌.
  5. 后端验证令牌并授予对资源的访问权限.

那是我所知道的正常流程.

That's the normal flow I know.

当我使用fire-base进行我项目的auth部分时,我无法理解这种正常的流程是如何变化的.

I can't understand how this normal flow alters when I use fire-base to do the auth part of the my project.

如何使用Firebase满足我的认证要求?

推荐答案

有两种情况.

首先,您从后端省略了用户创建部分,并使用Firebase身份验证在前端创建了用户帐户.该文档提供了让您入门的所有信息: https://firebase.google.com/docs /auth/web/start 例如使用电子邮件和密码身份验证时:

First, you omit the user creation part from the backend and create user accounts in the frontend using firebase authentication. The documentation has everything to get you startet: https://firebase.google.com/docs/auth/web/start E.g. when using email and password authentication:

firebase.auth().createUserWithEmailAndPassword(email, password).catch(function(error) {
  // Handle Errors here.
  var errorCode = error.code;
  var errorMessage = error.message;
  // ...
});

之后登录:

firebase.auth().signInWithEmailAndPassword(email, password).catch(function(error) {
  // Handle Errors here.
  var errorCode = error.code;
  var errorMessage = error.message;
  // ...
});

记住:

Firebase用户具有一组固定的基本属性-唯一ID, 主要电子邮件地址,名称和照片网址-存储在项目的 用户数据库,可由用户(iOS,Android,Web)更新. 您不能将其他属性直接添加到Firebase User对象. 相反,您可以将其他属性存储在Firebase中 实时数据库.

A Firebase User has a fixed set of basic properties—a unique ID, a primary email address, a name and a photo URL—stored in the project's user database, that can be updated by the user (iOS, Android, web). You cannot add other properties to the Firebase User object directly; instead, you can store the additional properties in your Firebase Realtime Database.

来自 https://firebase.google.com/docs/auth/users

第二,您可以通过使用nodejs中的firebase-admin创建用户帐户来通过后端路由用户创建.登录请求将直接从前端发送到Firebase(而不是后端).因此,后端仅用于用户创建/管理.

Second, you can route user creation through your backend by using firebase-admin in nodejs to create user accounts. Login requests will go from the frontend directly to firebase (and not to your backend). So the backend is just for user creation/management.

这篇关于Firebase,如何在node.js RESTapi后端上使用身份验证服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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