本文介绍了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;
  // ...
});

记住:

来自 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后端上使用身份验证服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 17:21