javascript - console.log and redirect in node.js doesn't work? -
i make application register , login, not working properly
this users.js routers/users.js
var express = require('express'); var router = express.router(); var passport = require('passport'); var localstrategy = require('passport-local').strategy; var multer = require('multer'); var upload = multer({dest: './uploads'}); var user = require('../models/user'); /* users listing. */ router.get('/', function(req, res, next) { res.send('respond resource'); }); router.get('/register', function(req, res, next) { res.render('register', { 'title': 'register' }); }); router.get('/login', function(req, res, next) { res.render('login', { 'title': 'login' }); }); router.post('/register', upload.single('profileimage'), function(req, res, next){ //get form values var name = req.body.name; var email = req.body.email; var username = req.body.username; var password = req.body.password; var password2 = req.body.password2; //check image field if(req.files && req.files.profileimage){ console.log('uploading file...'); //var profileimage = req.file.filename; //file info var profileimageoriginalname = req.files.profileimage.originalname; var profileimagename = req.files.profileimage.name; var profileimagemime = req.files.profileimage.mimetype; var profileimagepath = req.files.profileimage.path; var profileimageext = req.files.profileimage.extension; var profileimagesize = req.files.profileimage.size; } else { //set default image var profileimagename = 'noimage.png'; } //form validation req.checkbody('name','name field required').notempty(); req.checkbody('email','email field required').notempty(); req.checkbody('email','email not valid').isemail(); req.checkbody('username','username field required').notempty(); req.checkbody('password','password field required').notempty(); req.checkbody('password2','passwords not match').equals(req.body.password); //check errors var errors = req.validationerrors(); if(errors){ res.render('register', { errors: errors, name: name, email: email, username: username, password: password, password2: password2 }); } else { var newuser = new user({ name: name, email: email, username: username, password: password, profileimage: profileimagename }); //create user user.createuser(newuser, function(err, user){ if(err) throw err; console.log(user); }); //success message req.flash('success', 'you registered , may log in'); res.location('/'); res.redirect('/'); } }); passport.serializeuser(function(user, done) { done(null, user.id); }); passport.deserializeuser(function(id, done) { user.getuserbyid(id, function(err, user) { done(err, user); }); }); passport.use(new localstrategy( function(username, password, done){ user.getuserbyusername(username, function(err, user){ if(err) throw err; if(!user){ console.log('unknown user'); return done(null, false, {message: 'unknown user'}); } user.comparepassword(password, user.password, function(err, ismatch){ if(err) throw err; if(ismatch){ return done(null, user); } else { console.log('invalid password'); return done(null, false, {message: 'invalid password'}); } }); }); } )); router.post('/login', passport.authenticate('local',{failureredirect: '/users/login', failureflash: 'invalid username or password'}), function(req, res){ console.log('authentication successful'); req.flash('success', 'you logged in'); res.redirect('/'); }); module.exports = router;
and user.js models/user.js
var mongoose = require('mongoose'); var bcrypt = require('bcrypt'); mongoose.connect('mongodb://localhost/nodeauth'); var db = mongoose.connection; //user schema var userschema = mongoose.schema({ username: { type: string, index:true }, password: { type: string, required: true, bcrypt: true }, email: { type: string }, name: { type: string }, profileimage: { type: string } }); var user = module.exports = mongoose.model('user', userschema); module.exports.comparepassword = function(candidatepassword, hash, callback){ bcrypt.compare(candidatepassword, hash, function(err, ismatch){ if(err) return callback(err); callback(null, ismatch); }); } module.exports.getuserbyid = function(id, callback){ user.findbyid(id, callback); } module.exports.getuserbyusername = function(username, callback){ var query = {username: username}; user.findone(query, callback); } module.exports.createuser = function(newuser, callback){ bcrypt.hash(newuser.password, 10, function(err, hash){ if(err) throw err; //set hashed pw newuser.password = hash; //create user newuser.save(callback); }); }
when using registration, works, however, when using login not work. concretely not show me in cmd whether right or not logs, , redirect function not work.
ok, had quick passport
docs , looks authentication fails , redirected url specified in failureredirect
prop of passport.authenticate
method.
by default passport.authenticate
works so, if authentication fails, passport respond 401 unauthorized
status, , additional route handlers not invoked. since have specified options method, in particular, url redirect, passport redirection instead of responding 401 unauthorized
. shown on provided screen of command prompt. first 302 found
response post /users/login
afterwards redirected get /users/login
.
anyway, pay attention fact additional callbacks not invoked if authentication failed.
so, probably, never reached console.log
, res.redirect('/');
because never got authenticated.
Comments
Post a Comment