Below are the steps to creating authentication in nodejs with express, mongodb, and passport.
- npm install passport, passport-local, passport-local-mongoose, es6-promisify
1. Require mongoose, passport and es6-promisify into app.js
2. Created a handlers folder and created a file "passport.js".
- This file is used to configure the passport strategy
- Require passport, mongoose, and the model
- const passport = require('passport');
- const mongoose = require('mongoose');
- const User = mongoose.model('User');
- Create Strategy
- passport.use(User.createStrategy());
- Tell passport what to do with the actual User
- passport.serializeUser(User.serializeUser());
- passport.deserializeUser(User.deserializeUser());
3. Require /handlers/passport.js into app.js
4. Initialize two methods from passport into app.js
- app.use(passport.initialize());
- app.use(passport.session());
5. Create the User Schema
- /models/User.js