User Authentication API with PassportJS and ExpressJS

Tram Ho

Today started playing Nodejs, found it quite interesting and interesting, so I would like to share this article, guide for newbies to nodejs like me. The goal is to create a basic web with signup, login and logout functions for users, and of course you need to have basic Nodejs knowledge, as well as basic web.

Environment setup

  1. Create the project folder:
  • Here I will use the Express generator to build the base as well as the folder structure for this demo: 3
  • First you need to install npm install -g [email protected]
  • And run express passport-demo to initialize the demo.
  • Take a look at the folder tree, it’s quite basic, slowly I’ll go into detail
  1. Connect and use Mongodb:
  • Here, I will use ORM as Mongoose for Mongo, if you do not have experience or knowledge about it, read more here .
  • You need to install mongodb already.
  • and install mongoose to use: npm install mongoose.
  1. Install passport
  • npm install passport passport-local
  • Passport supports a lot of authen methods like with google, facebook, twiter … here I only use passport-local to authen user, you can refer to http://www.passportjs.org/docs /

Twist up your code and code

  1. First create the model user first:
  • Go straight into the code nhes
  • Set up for mongoose:
  • app.js

  • Create model user
  • models / user.js

I have commented in the code to explain quite well then, if you have any questions, please comment, I’ll try to explain if I know. =)) 2. Config for passport:

  • This is my passport configuration file: passport.js

  • Above if you do not understand how I use the verifed (done) function, please look at this code of passport local, have questions that can be answered below I will try to answer.

-Link github for anyone who wants to read and understand how it works https://github.com/jaredhanson/passport-local/blob/master/lib/strategy.js 3. Create routes:

  • Signup

  • Login:

  • Logout: This probably does not need api anymore, in the client we just delete the token obtained when logging in is complete.
  • The last step is also quite important to check the user is logged in, usually the client will use the token after login to send the cream in the header I will have the Auth.js file as follows

  • Express-jwt is a middleware that helps us validate the jwt token and set req.user from the retrieved token, see more at https://github.com/auth0/express-jwt
  • And now in the route you need to check which user is logged in just import and add the auth.required callback to check, I use userProperty: ‘payload’ so I want to get the encrypted payload in jwt we just need to: req.payload. id

summary

  • Above is the basic api demo app to create a registration and login function for users, the article is quite long, I write a lot of lack of hope for everyone to sympathize, hope in the next article I will try to solve prefer more and write full client side for you to understand.
  • If there’s anything wrong, please comment to exchange, thanks
Share the news now

Source : Viblo