Created
August 30, 2018 07:18
-
-
Save devarajchidambaram/061eb83501ddac10d86081a4cfffbc08 to your computer and use it in GitHub Desktop.
Passport js sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* EXPRESS SETUP */ | |
const express = require('express'); | |
const app = express(); | |
const bodyParser = require('body-parser'); | |
app.use(bodyParser.urlencoded({ | |
extended: true | |
})); | |
const mongoose = require('mongoose'); | |
mongoose.connect('mongodb://localhost/MyDatabase'); | |
const Schema = mongoose.Schema; | |
const UserDetail = new Schema({ | |
username: String, | |
password: String | |
}); | |
const UserDetails = mongoose.model('userInfo', UserDetail, 'userInfo'); | |
const passport = require('passport'); | |
app.use(require('express-session')({ | |
secret: 'keyboard cat', | |
resave: false, | |
saveUninitialized: false | |
})); | |
app.use(passport.initialize()); | |
app.use(passport.session()); | |
//We have to store the data in to req.session.passport={} object | |
passport.serializeUser(function(user, done) { | |
done(null, user.id); | |
}); | |
//We always need to deserilaize and validate the user | |
passport.deserializeUser(function(id, done) { | |
User.findById(id, function (err, user) { | |
done(err, user); | |
}); | |
}); | |
const LocalStrategy = require('passport-local').Strategy; | |
//There are lot of strategies are there so we can use any of the strategy for an example Oauth, twitter, facebook login mechanisams | |
passport.use(new LocalStrategy( | |
function (username, password, done) { | |
UserDetails.findOne({ | |
username: username | |
}, function (err, user) { | |
if (err) { | |
return done(err); | |
} | |
if (!user) { | |
return done(null, false); | |
} | |
if (user.password != password) { | |
return done(null, false); | |
} | |
return done(null, user); | |
}); | |
} | |
)); | |
app.get('/', | |
passport.authenticate('local', { | |
failureRedirect: '/error' | |
}), | |
function (req, res) { | |
res.redirect('/success?username=' + req.user.username); | |
}); | |
app.post('/', | |
passport.authenticate('local', { | |
failureRedirect: '/error' | |
}), | |
function (req, res) { | |
res.redirect('/success?username=' + req.user.username); | |
}); | |
app.get('/success', (req, res) => res.send("Welcome " + req.query.username + "!!")); | |
app.get('/error', (req, res) => res.send("error logging in")); | |
passport.serializeUser(function (user, cb) { | |
cb(null, user.id); | |
}); | |
passport.deserializeUser(function (id, cb) { | |
User.findById(id, function (err, user) { | |
cb(err, user); | |
}); | |
}); | |
app.get('/', (req, res) => res.sendFile('auth.html', { | |
root: __dirname | |
})); | |
const port = process.env.PORT || 3000; | |
app.listen(port, () => console.log('App listening on port ' + port)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment