I'm using a scotch.io tutorial on setting up PassportJS local login for an app I'm working on. After setting up my signup/login pages, MongoDB, PassportJS, I tried making a test "account", and.... nothing happens. No redirect, no flash message, nothing. It seems like nothing is getting picked up to post to the database. I'm including the pertinent scripts below. I tried to be as faithful as possible to the tutorial layout (source: https://scotch.io/tutorials/easy-node-authentication-setup-and-local).
index.js (entry point for server)
// load dependencies
var express = require('express');
var app = express();
var path = require('path');
var mongoose = require('mongoose');
var passport = require('passport');
var flash = require('connect-flash');
var LocalStrategy = require('passport-local').Strategy;
var morgan = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var session = require('express-session');
var configDB = require('./config/database.js');
// setup views
var ejs = require('ejs');
var engine = require('ejs-locals');
//database configuration
mongoose.connect(configDB.url);
require('./config/passport')(passport);
app.use(morgan('dev'));
app.use(cookieParser());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
//view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use('/public', express.static(path.join(__dirname, 'public')));
// required for passport
app.use(session({
secret: 'ilovescotchscotchyscotchscotch',
resave: true,
saveUninitialized: true
}));
app.use(passport.initialize());
app.use(passport.session()); // persistent login sessions
app.use(flash()); // for flash messages stored in-session
// routes
require('./routes')(app, passport);
app.listen(3000, function () {
console.log('Example app listening on port 3000!')
});
passport.js (passport settings)
var LocalStrategy = require('passport-local').Strategy;
var User = require('../user');
module.exports = function(passport) {
passport.serializeUser(function(user, done) {
done(null, user.id);
});
passport.deserializeUser(function(id, done) {
User.findById(id, function(err, user) {
done(err, user);
});
});
passport.use('local-signup', new LocalStrategy({
usernameField: 'email',
passwordField: 'password',
passReqToCallback: true
},
function(req, email, password, done) {
process.nextTick(function() {
User.findOne({ 'local.email': email }, function(err, user) {
if (err)
return done(err);
if (user) {
return done(null, false, req.flash('signupMessage', 'That e-mail address is already taken.'));
} else {
var newUser = new User;
newUser.local.email = email;
newUser.local.password = newUser.generateHash(password);
newUser.save(function(err) {
if (err)
throw err;
return done(null, newUser);
});
}
});
});
}));
passport.use('local-login', new LocalStrategy({
usernameField: 'email',
passwordField: 'password',
passReqToCallback: true
},
function(req, email, password, done) {
User.findOne({ 'local.email': email }, function(err, user) {
if (err)
return done(err);
if (!user)
return done(null, false, req.flash('loginMessage', 'User not found.'));
if(!user.validPassword(password))
return done(null, false, req.flash('loginMessage', 'Invalid password, please try again.'));
return done(null, user);
});
}));
};
/config/database.js
module.exports = {
'url': 'mongodb://localhost:27017/users/'
}
routes.js
module.exports = function(app, passport) {
app.get('/', function (req, res) {
res.render('pages/index');
});
app.get('/login', function (req, res) {
res.render('pages/login', { message: req.flash('loginMessage') });
});
app.get('/signup', function (req, res) {
res.render('pages/signup', { message: req.flash('signupMessage') });
});
app.post('/signup', passport.authenticate('local-signup', {
successRedirect: '/profile',
failureRedirect: '/signup',
failureFlash: true
}));
app.post('/login', passport.authenticate('local-login', {
successRedirect: '/profile',
failureRedirect: '/login',
failureFlash: true
}));
app.get('/profile', isLoggedIn, function(req, res) {
res.render('profile.ejs', {
user: req.user
});
});
app.get('/addroom', function (req, res) {
res.render('pages/addroom.ejs', {
message: req.flash('addroomMessage')
});
});
app.get('/propconfig', function (req, res) {
res.render('pages/propconfig.ejs', {
message: req.flash('propconfigMessage')
});
});
app.get('/logout', function(req, res) {
req.logout();
res.redirect('/');
});
};
// route middleware to ensure a user is logged in
function isLoggedIn(req, res, next) {
if (req.isAuthenticated())
return next();
res.redirect('/');
}
user.js (user schema)
// load dependencies
var mongoose = require('mongoose');
var bcrypt = require('bcrypt');
// define user schema
var userSchema = mongoose.Schema({
local: {
email: String,
password: String
}
});
// METHODS
// generating a hash
userSchema.methods.generateHash = function(password) {
return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null);
};
// checking if password is valid
userSchema.methods.validPassword = function(password) {
return bcrypt.compareSync(password, this.local.password);
};
// creating model for user to expose to app
module.exports = mongoose.model('User', userSchema);
and finally, signup.ejs (template for signup UI)
<!doctype html>
<html>
<head>
<title>Node Authentication</title>
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.2/css/bootstrap.min.css"> <!-- load bootstrap css -->
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.min.css"> <!-- load fontawesome -->
<style>
body { padding-top:80px; }
</style>
<script src="https://code.jquery.com/jquery-3.1.1.min.js" integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8=" crossorigin="anonymous"></script>
<script src="http://code.jquery.com/ui/1.12.1/jquery-ui.min.js" integrity="sha256-VazP97ZCwtekAsvgPBSUwPFKdrwD3unUfSGVYrahUqU=" crossorigin="anonymous"></script>
</head>
<body>
<div class="container">
<div class="col-sm-6 col-sm-offset-3">
<h1><span class="fa fa-sign-in"></span> Signup</h1>
<!-- show any messages that come back with authentication -->
<% if (message.length > 0) { %>
<div class="alert alert-danger"><%= message %></div>
<% } %>
<!-- LOGIN FORM -->
<form action="/signup" method="post">
<div class="form-group">
<label>Email</label>
<input type="text" class="form-control" name="email" id="email">
<div id="emailError"></div>
</div>
<div class="form-group">
<label>Password</label>
<input type="password" class="form-control" name="password" id="password">
<div id="passwordError"></div>
</div>
<button type="submit" class="btn btn-warning btn-lg">Signup</button>
</form>
<hr>
<p>Already have an account? <a href="/login">Login</a></p>
<p>Or go <a href="/">home</a>.</p>
</div>
</div>
<script type="text/javascript" src="../public/javascripts/validate.js"></script>
</body>
</html>
via Matt
No comments:
Post a Comment