2014-08-28 54 views

回答

1

我这样做的方式是修改checkLoggedIn函数,以便当它重定向到/login时,它会传递一个名为redirect的查询字符串参数以及重定向到的路径。然后,在/login路线,添加以下功能:

app.route('/login') 
    .all(passport.authenticate('basic'), function(req, res) { 
     res.redirect(req.query.redirect); 
    }); 
0

要填写从taylorthomas

在我的包路线答案,查询字符串添加到URL

var checkLoggedin = function($q, $timeout, $http, $location) { 
     // Initialize a new promise 
     var deferred = $q.defer(); 
     var path = $location.path(); 

     // Make an AJAX call to check if the user is logged in 
     $http.get('/loggedin').success(function(user) { 
      // Authenticated 
      if (user !== '0') $timeout(deferred.resolve); 

      // Not Authenticated 
      else { 
       $timeout(deferred.reject); 
       // $location.url('/auth/login'+'?redirect='+path); 
       $location.url('/auth/login').search('redirect', path); 
      } 
     }); 

     return deferred.promise; 
    }; 

在packages/users/public/controllers/meanUser.js中添加重定向到服务器的呼叫/登录

angular.module('mean.users') 
.controller('LoginCtrl', ['$scope', '$rootScope', '$http', '$location', 
    function($scope, $rootScope, $http, $location) { 
     // This object will be filled by the form 
     $scope.user = {}; 
     var query = $location.search(); 

     // Register the login() function 
     $scope.login = function() { 
      var url = '/login'; 
      if (query.redirect) { 
       url = '/login?redirect=' + query.redirect; 
      } 
      $http.post(url, { 
       email: $scope.user.email, 
       password: $scope.user.password 
      }) 
      .success(function(response) { 
       // authentication OK 
       $scope.loginError = 0; 
       $rootScope.user = response.user; 
       $rootScope.$emit('loggedin'); 
       if (response.redirect) { 
        if (window.location.href === response.redirect) { 
         //This is so an admin user will get full admin page 
         window.location.reload(); 
        } else { 
         window.location = response.redirect; 
        } 
       } else { 
        $location.url('/'); 
       } 
      }) 
      .error(function() { 
       $scope.loginerror = 'Authentication failed.'; 
      }); 
     }; 
    } 
]) 
package/users/server/routes/users.js中的

add /#!到重定向查询

app.route('/login') 
    .post(passport.authenticate('local', { 
     failureFlash: true 
    }), function(req, res) { 
     if (req.query.hasOwnProperty('redirect')) { 
      // res.redirect(req.query.redirect); 
      var redirect = '/#!' + req.query.redirect; 
      res.send({ 
       user: req.user, 
       redirect: redirect 
      }); 
     } else { 
      res.send({ 
       user: req.user, 
       redirect: (req.user.roles.indexOf('admin') !== -1) ? req.get('referer') : false 
      }); 
     } 
    });