Intereting Posts

创build帐户电子邮件validation无效

我想这样做,当用户注册我的网站,他们得到一封电子邮件,说他们需要validation他们的电子邮件使用该网站。 它目前发送带有令牌的电子邮件,但是当我去到确认链接时,它失败, status 404

路线

 app.post('/confirmation/:token', users.confirmationPost); 

需要控制器

 var users = require('../controllers/users-controller'); 

confirmationPost

 exports.confirmationPost = function (req, res, next) { req.assert('email', 'Email is not valid').isEmail(); req.assert('email', 'Email cannot be blank').notEmpty(); req.assert('token', 'Token cannot be blank').notEmpty(); req.sanitize('email').normalizeEmail({ remove_dots: false }); // Check for validation errors var errors = req.validationErrors(); if (errors) return res.status(400).send(errors); // Find a matching token Token.findOne({ token: req.body.token }, function (err, token) { if(err) throw err; if (!token) return res.status(400).send({ type: 'not-verified', msg: 'We were unable to find a valid token. Your token my have expired.' }); // If we found a token, find a matching user User.findOne({ _id: token._userId }, function (err, user) { if(err) throw err; if (!user) return res.status(400).send({ msg: 'We were unable to find a user for this token.' }); if (user.isVerified) return res.status(400).send({ type: 'already-verified', msg: 'This user has already been verified.' }); // Verify and save the user user.isVerified = true; user.save(function (err) { if (err) { return res.status(500).send({ msg: err.message }); } res.status(200).send("The account has been verified. Please log in."); }); }); }); }; 

护照注册

 passport.use('signup', new LocalStrategy({ usernameField: 'email', passReqToCallback : true }, function(req, email, password, done) { var findOrCreateUser = function(){ User.findOne({ email: req.body.email }, function(err, existingUser) { if(err){ console.log(err); } if (existingUser) { req.flash('form', { email: req.body.email }); return done(null, false, req.flash('error', 'An account with that email address already exists.')); } // edit this portion to accept other properties when creating a user. var user = new User({ email: req.body.email, password: req.body.password // user schema pre save task hashes this password }); user.save(function(err) { if (err) return done(err, false, req.flash('error', 'Error saving user.')); var token = new Token({ _userId: user._id, token: crypto.randomBytes(16).toString('hex') }); token.save(function (err) { if (err) return done(null, false, req.flash('error', err.message)); // Send the email var message = 'Hello,\n\n' + 'Please verify your account by clicking the link: \nhttp:\/\/' + req.headers.host + '\/confirmation\/' + token.token + '.\n'; sendEmail('"Phantom Asset Management" noreply@phantomam.com', user.email, 'Account Verification Token', message); }); var time = 14 * 24 * 3600000; req.session.cookie.maxAge = time; //2 weeks req.session.cookie.expires = new Date(Date.now() + time); req.session.touch(); return done(null, user, req.flash('success', 'A verification email has been sent to ' + user.email + '.')); }); }); }; process.nextTick(findOrCreateUser); }) ); 

当我去/confirmation/:token我得到我的错误404页面,并没有在我的控制台。 只有我看到的是我的debugging器显示该路线的错误404。

Solutions Collecting From Web of "创build帐户电子邮件validation无效"