Firebase, how to use auth service with a node.js RESTapi backend

独自空忆成欢 提交于 2019-12-24 18:23:39

问题


I'm a full-stack java script developer.

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

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

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.

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. Pass details to the back-end to create a user account.
  2. Backend creates an user account.
  3. Then after when the user trying to logging credentials are pass to back-end and release a token from the back-end
  4. That token stores in the user's browser temporary memory and attached with each and every request he pass to the back-end.
  5. Back-end verify the token and gives access to the resources.

That's the normal flow I know.

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

HOW DO I ACHIEVE MY AUTH REQUIREMENT USING THE FIREBASE?


回答1:


There are two cases.

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;
  // ...
});

Login after that:

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

Remember:

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.

From https://firebase.google.com/docs/auth/users

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.



来源:https://stackoverflow.com/questions/52124576/firebase-how-to-use-auth-service-with-a-node-js-restapi-backend

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!