为什么res。redirect选项?

时间:2021-12-20 15:07:18

I have a basic login controller with a form that updates the $scope of the user. When they click a button the login() function is triggered.

我有一个基本的登录控制器和一个更新用户的$范围的表单。当它们单击一个按钮时,login()函数将被触发。

.controller('loginCtrl', ['$scope','$http',function($scope,$http) {

    $scope.user = {
        'username' : "",
        'password' : ""
    };

    $scope.login = function(){
        $http.post('/login', $scope.user).then(function (response){
            console.log(response.data);
        });

    }

}]);

And here is my routing for /login

这是我的路径选择/登录

app.post('/login', function(req,res,next){

    passport.authenticate('local-login', function(err, user, info){
        if (err){
            console.log(err);
            return next(err);
        } else if (!user){
            return res.send(info);
        } else {
            req.logIn(user, function(err){
                if (err){
                    return next(err);
                } else {
                    return res.redirect('/view1');
                }
            });
        }
    })(req,res,next);

});

and here is my passport local-login.

这是我的本地护照登录。

passport.use('local-login', new LocalStrategy({
    passReqToCallback: true // Allows us to pass back the entire request to the callback function
},
function (req, email, password, done){
    // Checks database via mongoose
    ,function (err,user){
        if (err){
            return done(err);
        } else {
            if (!user){
                return done(null, false, { message : 'No user found.'});
            } else if (!user.validPassword(password)) {
                return done(null, false, { message : 'Incorrect password'});
            } else {
                return done(null, user);
            }
        }
    });
}));

The main problem here is when the res.redirect('/view1') is triggered in my routing logic the controller is receiving the html page as the response.data instead of actually being redirected. ($http.post().then(function (response){});)

这里的主要问题是,当res.redirect('/view1')在我的路由逻辑中被触发时,控制器接收html页面作为响应。数据而不是被重定向。(http.post美元()。然后(函数(响应){ });)

If this is incorrect how should I actually be handling the redirecting to /view1 after the user has successfully logged in?

如果这是错误的,在用户成功登录后,我应该如何处理重定向到/view1 ?

1 个解决方案

#1


2  

As your are calling the backend from javascript, you basically do an ajax call behind scenes, which will not work with redirects and other HTTP Headers.

当您从javascript调用后端时,您基本上是在后台执行ajax调用,这不会与重定向和其他HTTP头一起工作。

In order to redirect the user to the correct page, you have to check the HTTP Status code in the response from your backend and redirect the user to the correct site via angular. Which could look something like this:

为了将用户重定向到正确的页面,您必须检查后端响应中的HTTP状态代码,并通过角度将用户重定向到正确的站点。大概是这样的:

if(response.status == 302) {
    $window.location.href = '/view1';
}

or instead of sending a redirect you can send a status update from the server like:

或者,不发送重定向,您可以从服务器发送状态更新,比如:

res.json({
    success: true,
    redirectTo: '/view1'
});

and parse it on the client:

并在客户端进行解析:

if(response.data.success){
   $window.location.href = response.data.redirectTo;
}

#1


2  

As your are calling the backend from javascript, you basically do an ajax call behind scenes, which will not work with redirects and other HTTP Headers.

当您从javascript调用后端时,您基本上是在后台执行ajax调用,这不会与重定向和其他HTTP头一起工作。

In order to redirect the user to the correct page, you have to check the HTTP Status code in the response from your backend and redirect the user to the correct site via angular. Which could look something like this:

为了将用户重定向到正确的页面,您必须检查后端响应中的HTTP状态代码,并通过角度将用户重定向到正确的站点。大概是这样的:

if(response.status == 302) {
    $window.location.href = '/view1';
}

or instead of sending a redirect you can send a status update from the server like:

或者,不发送重定向,您可以从服务器发送状态更新,比如:

res.json({
    success: true,
    redirectTo: '/view1'
});

and parse it on the client:

并在客户端进行解析:

if(response.data.success){
   $window.location.href = response.data.redirectTo;
}