Last active
May 24, 2023 15:41
-
-
Save vesse/9e23ff1810089bed4426 to your computer and use it in GitHub Desktop.
stackoverflow#26403853
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
doctype html5 | |
html | |
head | |
title Test Page | |
body | |
.error #{message} | |
form(action='/login' method='POST') | |
label Email | |
input(type='email' name='email') | |
label Password | |
input(type='password' name='password') | |
input(type='submit' value='Login') |
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
{ | |
"dependencies": { | |
"body-parser": "^1.9.0", | |
"connect-flash": "^0.1.1", | |
"cookie-parser": "^1.3.3", | |
"express": "^4.9.7", | |
"express-session": "^1.8.2", | |
"jade": "^1.7.0", | |
"passport": "^0.2.1", | |
"passport-local": "^1.0.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
var express = require('express'), | |
session = require('express-session'), | |
cookieParser = require('cookie-parser'), | |
bodyParser = require('body-parser'), | |
passport = require('passport'), | |
LocalStrategy = require('passport-local').Strategy, | |
flash = require('connect-flash'); | |
var app = express(); | |
// Test user so don't need database | |
var USER = { | |
email: '[email protected]', | |
validPassword: function(passwd) { | |
return passwd === 'test'; | |
} | |
}; | |
var getUser = function(email, cb) { | |
if (email !== '[email protected]') | |
return cb(null, null); | |
cb(null, USER); | |
}; | |
passport.serializeUser(function(user, cb) { | |
cb(null, user.email); | |
}); | |
passport.deserializeUser(function(email, cb) { | |
cb(null, USER); | |
}); | |
passport.use('local-login', new LocalStrategy({ | |
usernameField : 'email', | |
passwordField : 'password' | |
}, | |
function(email, password, done) { | |
getUser(email, function(err, user) { | |
if (err) | |
return done(err); | |
if (!user) | |
return done(null, false, {message: 'Pas d\'utilisateur avec ce login.'}); | |
if (!user.validPassword(password)) | |
return done(null, false, {message: 'Oops! Mauvais password.'}); | |
return done(null, user); | |
}); | |
})); | |
app.use(cookieParser()); | |
app.use(bodyParser.urlencoded({ extended: false })) | |
app.use(bodyParser.json()); | |
app.use(session({ | |
secret: 'secret cat', | |
resave: true, | |
saveUninitialized: true | |
})); | |
app.use(flash()); | |
app.use(passport.initialize()); | |
app.use(passport.session()); | |
app.get('/login', function(req, res) { | |
res.render('login.jade', {message: req.flash('error')}); | |
}); | |
app.post('/login', passport.authenticate('local-login', { | |
successRedirect : '/profile', | |
failureRedirect : '/login', | |
failureFlash : true | |
})); | |
app.listen(3000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment