Skip to content

Instantly share code, notes, and snippets.

@vesse vesse/login.jade
Last active Jun 2, 2019

Embed
What would you like to do?
stackoverflow#26403853
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')
{
"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"
}
}
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: 'test@test.com',
validPassword: function(passwd) {
return passwd === 'test';
}
};
var getUser = function(email, cb) {
if (email !== 'test@test.com')
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
You can’t perform that action at this time.