Using Scotch.io and the official passport docs as reference, I'm implementing a Node/Express/Sequelize application where I have a basic signup form with fields for email and password:
<form action="/signup" method="post">
<div class="form-group">
<label>Email</label>
<input type="text" class="form-control" name="email">
</div>
<div class="form-group">
<label>Password</label>
<input type="password" class="form-control" name="password">
</div>
<button type="submit" class="btn btn-warning btn-lg">Signup</button>
</form>
with the passport-local strategy defined as follows:
const db = require('./sequelize');
const passport = require('passport'),
LocalStrategy = require('passport-local').Strategy;
passport.serializeUser(function(user, done) {
done(null, user.id);
});
passport.deserializeUser(function(user, done) {
db.User.findById(id, function(err, user) {
done(err, user);
});
});
passport.use('local', new LocalStrategy({
emailField : 'email',
passwordField : 'password',
passReqToCallback : true
},
function(email, password, done){
process.nextTick(function() {
// find a user whose email is the same as the forms email
// we are checking to see if the user trying to login already exists
db.User.findOne({ email : email }, function(err, user) {
// if there are any errors, return the error
if (err)
return done(null, false, {message: err});
// check to see if theres already a user with that email
if (user) {
return done(null, false, {message: 'User already exists'});
}
else {
// if there is no user with that email
// create the user
db.User.create({
email: email,
password: password
});
return done(null, false, {message: 'User created'});
}
});
});
}
));
module.exports = passport;
and lastly the routes are taken care of in
const express = require('express');
const http = require('http');
const https = require('https');
const sequelize = require('sequelize');
const db = require('./config/sequelize');
const querystring = require('querystring');
const config = require('./config/config');
const passport = require('./config/passport');
const bodyParser = require( 'body-parser' );
const env = 'development';
const app = express();
const port = 3000;
app.use( bodyParser.urlencoded({ extended: true }) );
app.use(passport.initialize());
app.use(passport.session());
app.set('view engine', 'ejs');
app.listen(port);
app.post('/signup', function(req, res, next){
const user = req.body;
passport.authenticate('local', function(err, user, info){
if (err){
res.send({success: false, message: 'authentication failed'});
}
else{
console.log(info);
res.json({message: "success"});
}
})(req, res, next)
});
What I expect to happen if the local strategy works is for the res.json({message: "success"}) to appear on the browser and for the database to be updated per the specifications of ./config/passport.js. The {message: "success"} is appearing on the screen, but the database is not being updated, which leads me to believe that something is wrong with my implementation in ./config/passport.js. In app.js, the console.log(info) statement shows the following message on cmd:
Executing (default): SELECT `id`, `username`, `email`, `password`, `createdAt`,
`updatedAt` FROM `Users` AS `User` LIMIT 1;
which makes it look like the insertions might be going through but inspections of the db reveal otherwise. I have done previous sanity checks with CRUD updates to confirm that the basic implementation of Sequelize works, and will provide the schema of the table upon request. How else can I debug this situation?
Read about how to make queries in sequelize http://docs.sequelizejs.com/en/latest/docs/querying/ you should use promises, like this:
db.User.findOne({ where: {email : email }})
.then(function(user){
//do smth;
})
.catch(function(error){
console.log(error);
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.