My passport.authenticate request is returning a 404 for some reason, and I have literally no clue why. My routes are definitely correct, pretty sure (de)serialization is correct and user details do get stored in database; however, returns a 404 when details ARE stored, and a 401 when details are incorrect (as expected). I am using mongodb/mongoose Any help would be appreciated.
passport.js:
const passport = require('passport');
const User = require('../models/user');
const LocalStrategy = require('passport-local').Strategy;
passport.serializeUser((user, done) => {
done(null, user.id);
});
passport.serializeUser((id, done) => {
User.findById(id, (err, user) => {
done(err, user);
})
});
passport.use('local.register', new LocalStrategy({
usernameField: 'username',
passwordField: 'password',
passReqToCallback: true
}, (req, username, password, done) => {
User.findOne({ $or: [{'email': req.body.email}, {'username': username}]}, (err, user) => {
if(err || user) {
return done(err);
}
if(user) {
return done(null, false, {message: 'Email or username in use'});
}
const newUser = new User();
newUser.username = username;
newUser.email = req.body.email;
newUser.password = newUser.encryptPassword(password);
newUser.save((err, data) => {
if(err) {
return done(err);
}
//CODE REACHES THIS POINT, HAVE DEBUGGED
return done(null, newUser);
});
});
}));
user.js:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const bcrypt = require('bcrypt-nodejs');
const userSchema = new Schema({
username: {type: String, required: true},
email: {type: String, required: true},
password: {type: String, required: true},
});
userSchema.methods.encryptPassword = function() {
return bcrypt.hashSync(this.password, bcrypt.genSaltSync(5), null);
};
userSchema.methods.validPassword = function(password) {
return bcrypt.compareSync(this.password, password);
};
module.exports = mongoose.model('User', userSchema);
registration.js:
const express = require('express');
const router = express.Router();
const mongodb = require('mongodb');
const passport = require('passport');
router.post('/', passport.authenticate('local.register'));
module.exports = router;
app.js:
const express = require('express');
const path = require('path');
const logger = require('morgan');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const sassMiddleware = require('node-sass-middleware');
const cors = require('cors');
const mongoose = require('mongoose');
const mongo = require('mongodb');
const passport = require('passport');
const python = require('./routes/python');
const register = require('./routes/registration');
const app = express();
const dbURL = 'mongodb://localhost:27017/aiarena';
mongoose.connect(dbURL);
require('./config/passport');
app.use(cors());
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
// uncomment after placing your favicon in /public
// app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cookieParser());
app.use(passport.initialize());
app.use(passport.session());
app.use(sassMiddleware({
src: path.join(__dirname, 'public'),
dest: path.join(__dirname, 'public'),
indentedSyntax: true, // true = .sass and false = .scss
sourceMap: true,
}));
//Routes
app.use('/', express.static(path.join(__dirname, '../client/')));
app.use('/python', python);
app.use('/register', register);
...
via Fraser Price
No comments:
Post a Comment