如果我点击子页面中的登录按钮,则会重新加载子页面和父页面

时间:2016-09-30 06:44:51

标签: angularjs node.js mongodb express

我正在使用angular.js和node.js重置密码。在父页面中,我点击了忘记密码链接,然后输入了电子邮件地址。一个链接将发送到该邮件。

登录页面

login.html

忘记密码页

forgot password.html

点击我的重置密码按钮后,我收到一封电子邮件,其中包含重置密码页面的链接。

重置密码页

Reset page

点击链接重置 - 密码页面打开后我更改了密码,显示成功消息。

重置密码页

Reset

在此之后,我点击Go back to login链接,然后将此子页面重定向到登录页面。同时父页面也从忘记密码页面重定向到登录页面。从子页面成功登录后,在父页面中,url栏显示如下http://localhost:3000/pages/auth/login?username=&password=kevin。它在父页面url bar上显示我的密码。所以现在我想做父页面没有被重定向。

login.module

(function ()
{
'use strict';

angular
    .module('app.pages.auth.login', [])
    .config(config);

/** @ngInject */
function config($stateProvider, $translatePartialLoaderProvider)
{
    $stateProvider.state('app.pages_auth_login', {
        url  : '/pages/auth/login',
        views: {
            'main@'                       : {
                templateUrl: 'app/core/layouts/basic.html'
            },
            'content@app.pages_auth_login': {
                templateUrl: 'app/main/pages/auth/login/login.html',
                controller : 'LoginController as vm'
            }
        }
    });

    $translatePartialLoaderProvider.addPart('app/main/pages/auth/login');
}

})();

复位password.module

(function ()
{
'use strict';

angular
    .module('app.pages.auth.reset-password', [])
    .config(config);

/** @ngInject */
function config($stateProvider, $translatePartialLoaderProvider)
{
    $stateProvider.state('app.pages_auth_reset-password', {
        url  : '/pages/auth/reset-password?token',
        views: {
            'main@'                          : {
                templateUrl: 'app/core/layouts/basic.html'
            },
            'content@app.pages_auth_reset-password': {
                templateUrl: 'app/main/pages/auth/reset-password/reset-password.html',
                controller : 'ResetpasswordController as vm'
            }
        }
    });
}

})();

忘记-password.module

(function ()
{
'use strict';

angular
    .module('app.pages.auth.forgot-password', [])
    .config(config);

/** @ngInject */
function config($stateProvider, $translatePartialLoaderProvider)
{
    $stateProvider.state('app.pages_auth_forgot-password', {
        url  : '/pages/auth/forgot-password',
        views: {
            'main@'                                 : {
                templateUrl: 'app/core/layouts/basic.html'
            },
            'content@app.pages_auth_forgot-password': {
                templateUrl: 'app/main/pages/auth/forgot-password/forgot-password.html',
                controller : 'ForgotPasswordController as vm'
            }
        }
    });

    $translatePartialLoaderProvider.addPart('app/main/pages/auth/forgot-password');
}

})();

api.js

router.post('/pages/auth/forgot-password', function(req,res,next){
    var maillist = req.body.email;
    async.waterfall([
        function(done) {
            crypto.randomBytes(20, function(err, buf) {
                var token = buf.toString('hex');
                done(err, token);
            });
        },

        function(token, done) {
            User.findOne({ email : maillist}, function(err, user) {
                if (!user){
                    var message = {
                        message: 'No account with this email address exists.'
                    };
                    done(null, message);
                } else if (token, user, done){
                    user.resetPasswordToken = token;
                    user.resetPasswordExpires = Date.now() + 3600000;           

                    user.save(function(err) {
                        done(err, token, user);
                    });

                    var mailOptions={
                            to : maillist,
                            subject : 'Password Recovery',                  
                            text: 'Dear ' + maillist + ' \n\n' +
                            'You are receiving this because you (or someone else) have requested the reset of the password for your account.\n\n' +
                            'Please click on the following link, or paste this into your browser to complete the process:\n\n' +
                            'http://127.0.0.0:3000/pages/auth/reset-password?' +'token='+ token + '\n\n' +
                            'If you did not request this, please ignore this email and your password will remain unchanged.\n'
                        };
                        transport.sendMail(mailOptions, function(error, response){
                            if(!error){

                            }
                            transport.close();
                        });
                        var message = {
                            message: 'We have sent a link on ' + maillist + ' to reset your password and you should receive it shortly.'
                        };
                        done(null, message);
                    }
            });
        }

    ], function(err, result){
        if (err) return next(err);
        return res.json({
            result: result.message
        });
    });
});

router.get('/pages/auth/reset-password/:token', function(req, res) {
    User.findOne({ resetPasswordToken: req.query.tkn, resetPasswordExpires: { $gt: Date.now() }}, function(err, user) {
        if (!user){
            //req.flash('error', 'Password reset token is invalid or has expired.');
            //return res.redirect('http://127.0.0.0:3000/api/pages/auth/forgot-password');
            return res.json({result: 'Password reset token is invalid or has expired.'});
        }
        return res.redirect('http://127.0.0.0:3000/api/pages/auth/reset-password/:token', {
          user: req.user
        });
    });
});

router.post('/pages/auth/reset-password/:token', function(req, res){
    var tkn = req.body.tkn;
    async.waterfall([
        function(done) {
            User.findOne({ resetPasswordToken: req.body.tkn, resetPasswordExpires: { $gt: Date.now() } }, function(err, user) {
                if (!user) {
                    var message = {
                        message: 'Password reset token is invalid or has expired.'
                    };
                    done(null, message);
                }else if (user, done){
                    user.password = req.body.data.newPassword;
                    user.resetPasswordToken = undefined;
                    user.resetPasswordExpires = undefined;

                    user.save(function(err) {
                        req.logIn(user, function(err) {
                            done(err, user);
                        });
                    });

                    var mailOptions={
                        to :  user.email,
                        subject : 'Your password has been changed',                 
                        text: 'Hello,\n\n'+
                        'This is a confirmation that the password for your account ' + user.email + ' has just been changed.\n'
                    };
                    transport.sendMail(mailOptions, function(error, response){
                        if(!error){

                        }
                        transport.close();
                    });
                    var message = {
                        message: 'Success! Your password has been changed. Click below link to login.'
                    };
                    done(null, message);
                }
            });
        }
    ], function(err, result){
        if (err) return next(err);
        return res.json({
            result: result.message
        });
    });
});

0 个答案:

没有答案