Created
November 27, 2014 07:17
-
-
Save maisnamraju/1f4846db3c519ee55513 to your computer and use it in GitHub Desktop.
passport local authentication in express
This file contains 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
var config = require('./config'); | |
var passport = require('passport'); | |
var User = require('./models/user'); | |
var LocalStrategy = require('passport-local').Strategy; | |
var isValidPassword = function(user, password){ | |
return bCrypt.compareSync(password, user.password); | |
}; | |
// Generates hash using bCrypt | |
var createHash = function(password){ | |
return bCrypt.hashSync(password, bCrypt.genSaltSync(10), null); | |
} | |
// As with any middleware it is quintessential to call next() | |
// if the user is authenticated | |
var isAuthenticated = function (req, res, next) { | |
if (req.isAuthenticated()) | |
return next(); | |
res.redirect('/'); | |
} | |
passport.use('signup', new LocalStrategy({ | |
passReqToCallback : true | |
}, | |
function(req, email, password, done) { | |
findOrCreateUser = function(){ | |
// find a user in Mongo with provided email | |
User.findOne({'email':email},function(err, user) { | |
// In case of any error return | |
if (err){ | |
console.log('Error in SignUp: '+err); | |
return done(err); | |
} | |
// already exists | |
if (user) { | |
User.findOne({ 'email' : email }, | |
function(err, user) { | |
if (!user){ | |
console.log('User Not Found with email '+email); | |
return done(null, false); | |
} | |
// User exists but wrong password, log the error | |
if (!isValidPassword(user, password)){ | |
console.log('Invalid Password'); | |
return done(null,false); | |
} | |
}); | |
} else { | |
// if there is no user with that email | |
// create the user | |
var newUser = new User(); | |
// set the user's local credentials | |
newUser.email = email; | |
newUser.password = createHash(password); | |
// save the user | |
newUser.save(function(err) { | |
if (err){ | |
console.log('Error in Saving user: '+err); | |
throw err; | |
} | |
console.log('User Registration succesful'); | |
return done(null, newUser); | |
}); | |
} | |
}); | |
}; | |
// Delay the execution of findOrCreateUser and execute | |
// the method in the next tick of the event loop | |
process.nextTick(findOrCreateUser); | |
}) | |
); |
This file contains 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
'use strict' | |
var express = require('express'); | |
var passport = require('passport'); | |
var middleware = require('./routehandlers/middleware'); | |
var index = require('./routehandlers/index'); | |
var auth = require('./routehandlers/auth'); | |
var me = require('./routehandlers/me'); | |
var appointments = require('./routehandlers/appointments'); | |
var router = express.Router(); | |
// Index | |
router.get('/', function(req, res) { | |
res.render('index'); | |
}); | |
// Me | |
router.get('/me', middleware.ensureAuthenticated, me); | |
router.get('/timeslot',function(req,res) { | |
var timeslots = [ | |
{ | |
time:1, | |
description: "Welcome to my scheduling page. Please follow the instructions to add an event to my calendar.", | |
cost: "10" | |
}, | |
{ | |
time:2, | |
description: "Welcome to my scheduling page. Please follow the instructions to add an event to my calendar.", | |
cost: "20" | |
}, | |
{ | |
time:3, | |
description: "Welcome to my scheduling page. Please follow the instructions to add an event to my calendar.", | |
cost: "10" | |
}, | |
{ | |
time:4, | |
description: "Here lies 3", | |
cost: "10" | |
} | |
]; | |
res.render('timeslot', { timeslot: JSON.stringify(timeslots) }); | |
}); | |
router.post('/signup', function(req, res) { | |
passport.authenticate('signup', { | |
successRedirect: '/timeslot', | |
failureRedirect: '/' | |
}); | |
console.log('here'); | |
}); | |
// Appointments | |
router.route('/appointments') | |
.all(middleware.ensureAuthenticated) | |
.get(appointments.getByUser) | |
.post(middleware.sanitizeRequestBody, appointments.create); | |
router.route('/appointments/:id') | |
.all(middleware.ensureAuthenticated) | |
.get(appointments.getById) | |
.put(middleware.sanitizeRequestBody, appointments.update) | |
.patch(middleware.sanitizeRequestBody, appointments.update) | |
.delete(appointments.delete); | |
// -- | |
module.exports.router = router; |
This file contains 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
var express = require('express'); | |
var bodyParser = require('body-parser'); | |
var leisure = require('leisure'); | |
var cors = require('cors'); | |
var passport = require('passport'); | |
var config = require('./config'); | |
var passportConfig = require('./passport-config'); | |
var session = require('express-session') | |
var expressHbs = require('express-handlebars'); | |
var mediaTypes = [ | |
{ contentType: 'application/hal+json' }, | |
{ contentType: 'application/json' }, | |
{ contentType: 'text/html' } | |
]; | |
var app = express(); | |
/*Handlebars */ | |
app.engine('handlebars', expressHbs({layout: false}) ); | |
app.set('view engine', 'handlebars'); | |
app.use(express.static(__dirname + '/assets')); | |
app.use(cors(config.settings.cors)); | |
app.use(bodyParser()); | |
app.use(leisure.accept(mediaTypes)); | |
/*sessions */ | |
app.use(session({ | |
secret: 'keyboardSFS23432@@!#!@at' | |
})); | |
app.use(passport.initialize()); | |
app.use(passport.session()); | |
var routes = require('./routes'); | |
app.use('/', routes.router); | |
function start () { | |
var port = process.env.PORT || 3000; | |
app.listen(port); | |
console.log('Appoints service started on port ' + port); | |
} | |
exports.app = app; | |
exports.start = start; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment