Monday 25 January 2016

Redirecting from POST [Express 4]

Hi! In this piece of routes code I'm trying to make a different flash-messages appear depending on user input (correct or wrong pass; if correct -> change current pass, show flash-message).The problem is that I can't redirect from POST. How it can be done? var flash = require('connect-flash'); var User = require('../app/models/user'); app.all('/change-info', function (req, res) { if(req.user.validPassword(req.body.oldPassword)){ User.findOneAndUpdate({'local.password': req.user.local.password}, {'local.password': req.user.generateHash(req.body.newPassword)}, function (err, user) { console.log(user); }); res.redirect('/flash-success'); }else{ res.redirect('/flash-failure'); } res.send(); }); app.get('flash-failure', function (req, res) { req.flash('pass-changed', 'Wrong current password'); res.redirect('/profile'); }); app.get('/flash-success', function (req, res) { req.flash('pass-changed', 'Password successfully changed'); res.redirect('/profile'); }); app.get('/profile', isLoggedIn, function(req, res) { res.render('profile.ejs', { user: req.user, message: req.flash('pass-changed') }); });

Submitted January 25, 2016 at 10:49AM by GodOfTheMetal

No comments:

Post a Comment