Angular 7中的密码重置实现

时间:2019-02-27 21:38:37

标签: node.js angular post reset-password

我正在跟踪MEAN堆栈,并且能够使用URL中的jwt令牌发送重置密码电子邮件。密码重置链接的格式为http://localhost:4200/reset/token。当单击链接并发送帖子请求以更改密码时,如何以角度检索此令牌?发送电子邮件的后端实现->

    function(done){
        Usermodel.findOne({email: req.body.email}, function(err, user){
            if(!user){
                return res.status(422).send({errors: [{title: 'Invalid email!', detail: 'User does not exist'}]});
            }

            const token = jwt.sign({
                userId: user.id,
                username: user.username,
                resetPasswordToken: user.resetPasswordToken
              }, config.SECRET, { expiresIn: '1h' });

            user.resetPasswordToken = token;
            user.resetPasswordExpires = Date.now() + 3600000; // 1 hour

            user.save(function(err){
                done(err, token, user);
            });
        });
    },
    function(token, user, done){
        const smtpTransport = nodemailer.createTransport({
            service: 'Gmail',
            auth: {
                user: 'XXXX',
                pass: 'XXXX'
            }
        });
            function(token, user, done){
        const smtpTransport = nodemailer.createTransport({
            service: 'Gmail',
            auth: {
                user: 'XXXX',
                pass: 'XXXX'
            }
        });

        const mailOptions = {
            to: user.email,
            from: 'xxxx@gmail.com',
            subject: 'Nodejs password reset',
            text: 'You are receiving this email. Please click on the email for password reset ' +
                  'http://' + req.headers.host + '/reset/' + token + '\n\n' + 
                  'If you did not request this, please ignore this email' 

        };
        smtpTransport.sendMail(mailOptions, function(err){
            console.log('mail sent');
            done(err, 'done');
        });
    }

// Bakend实现使用令牌重置密码

    function(done) {
        Usermodel.findOne({ resetPasswordToken: req.params.token, resetPasswordExpires: { $gt: Date.now() } }, function(err, user){
            if(!user){
                return res.status(422).send({errors: [{title: 'error', detail: 'Password reset token is invalid or has expired'}]});
            }

            if(req.body.password === req.body.confirm){
                user.setPassword(req.body.password, function(err){
                    user.resetPasswordToken = undefined;
                    user.resetPasswordExpires = undefined;

                    user.save(function(err){
                        req.logIn(user, function(err) {
                            done(err, user);
                        });
                    });
                });
            } else {
                return res.status(422).send({errors: [{title: 'error', detail: 'Password do not match'}]});
            }
        });
    },
    function(user, done){
        var smtpTransport = nodemailer.createTransport({
            service: 'Gmail',
            auth: {
                user: 'XXXX',
                pass: 'XXXX'
            }
        });

        var mailOptions = {
            to: user.email,
            from: 'xxxx@gmail.com',
            subject: 'Your password has been changed',
            text: 'Hello,\n\n' + 
                'This is a confirmation that the password for your account ' + user.email + ' has just changed'
        };
        smtpTransport.sendMail(mailOptions, function(err){
            done(err);
        });
    }
],   function(err){
});

});

0 个答案:

没有答案