Last active
December 15, 2015 08:59
-
-
Save arunk/5235528 to your computer and use it in GitHub Desktop.
passport-local-mongoose simple config
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
/** | |
* Module dependencies. | |
*/ | |
var express = require('express') | |
, routes = require('./routes') | |
, user = require('./routes/user') | |
, http = require('http') | |
, path = require('path') | |
, passport = require('passport'), | |
, User = require('./user').User; | |
var app = express(); | |
app.configure(function(){ | |
app.set('port', process.env.PORT || 3000); | |
app.set('views', __dirname + '/views'); | |
app.set('view engine', 'jade'); | |
app.use(express.favicon()); | |
app.use(express.logger('dev')); | |
app.use(express.bodyParser()); | |
app.use(express.methodOverride()); | |
app.use(express.cookieParser('your secret here')); | |
app.use(express.session()); | |
app.use(passport.initialize()); | |
app.use(passport.session()); | |
app.use(app.router); | |
app.use(require('less-middleware')({ src: __dirname + '/public' })); | |
app.use(express.static(path.join(__dirname, 'public'))); | |
}); | |
passport.use(User.createStrategy()); | |
passport.serializeUser(User.serializeUser()); | |
passport.deserializeUser(User.deserializeUser()); | |
app.configure('development', function(){ | |
app.use(express.errorHandler()); | |
}); | |
app.get('/setval', function(req, res) { | |
req.session.name = 'arun'; | |
res.redirect('/getval'); | |
}); | |
app.get('/getval', function(req, res) { | |
res.render('index', { title: req.session.name }); | |
}); | |
app.get('/login', function(req, res) { | |
res.render('login', { title: "Login"}); | |
}); | |
app.post('/login', passport.authenticate('local'), function(req, res) { | |
res.redirect('/authtest'); | |
}); | |
app.get('/authtest', passport.authenticate('local'), function(req, res){ | |
console.log("auth test success"); | |
res.render('index', { title: "auth success"}); | |
}); | |
app.get('/authdebug', function(req,res,next) { | |
passport.authenticate('local', | |
function(err, user) { | |
console.log("err:" + err); | |
console.log("user: "+ user); | |
if (err) { return next(err); } | |
if (!user) { res.redirect('/login'); } | |
req.logIn(user, function(err) { | |
console.log("error on login: " + err); | |
if (err) { return next(err); } | |
return res.redirect('/getval'); | |
}); | |
})(req,res,next); | |
}); | |
app.post('/register', function(req, res) { | |
User.register( | |
new User({ username: req.body.username }), | |
req.body.password, | |
function(err, acct) { | |
if (err) { | |
res.json({status: false}); | |
} else { | |
res.json({status: true, account: acct}); | |
} | |
} | |
); | |
}); | |
http.createServer(app).listen(app.get('port'), function(){ | |
console.log("Express server listening on port " + app.get('port')); | |
}); |
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
{ | |
"name": "application-name", | |
"version": "0.0.1", | |
"private": true, | |
"scripts": { | |
"start": "node app" | |
}, | |
"dependencies": { | |
"express": "3.1.0", | |
"jade": "*", | |
"less-middleware": "*", | |
"passport": "*", | |
"passport-local": "*", | |
"passport-local-mongoose": "*", | |
"mongoose": "3.6.0" | |
} | |
} |
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
(function() { | |
var UserSchema, config, mongoose, passportLocalMongoose; | |
mongoose = require('mongoose'); | |
passportLocalMongoose = require('passport-local-mongoose'); | |
mongoose.connect('SERVER_IP_OR_DNS_REPLACE_ME', 'DB_NAME_REPLACE_ME'); | |
UserSchema = new mongoose.Schema({ | |
email: String, | |
firstName: String, | |
lastName: String, | |
location: [Number], | |
ts: Date | |
}); | |
UserSchema.plugin(passportLocalMongoose); | |
module.exports.User = mongoose.model('User', UserSchema); | |
}).call(this); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment