尝试在我的Angular应用程序中使用passport.js来使用谷歌身份验证 .
我设置快速 endpoints 的方式是:
app.set('client-google-signin-path', '/google');
app.route('/auth/google')
.get(function(req, res, next) {
var mobileApp = req.headers.origin;
passport.authenticate('google', { scope: ['profile', 'email'], callbackURL: mobileApp + req.app.get('client-google-signin-path')})(req, res, next);
});
app.route('/auth/google/callback')
.get(function(req, res, next) {
//NEVER MAKES IT HERE
var mobileApp = req.headers.origin;
passport.authenticate('google', {scope: ['profile', 'email'], callbackURL: mobileApp + req.app.get('client-google-signin-path')},
function(err, user, info) {
var profile;
if(err) return next(err);
if(!info || !info.profile) return res.status(400).send("Profile not available.");
// Grab the profile info returned from google.
profile = info.profile._json;
//model logic
User.findOne({ 'email': profile.email }, function(err, user) {
if(err) return next(err);
if(!user) return res.status(400).send("There does not appear to be an account that matches, you may need to create an account and try again.");
req.logIn(user, function(err) {
if(err) return next(err);
return res.status(200).send(user);
});
});
res.status(200);
})(req, res, next);
});
我的控制器看起来像:
$scope.signInGoogle = function() {
return $http.get(backendConst.location + 'auth/google')
.success(function(url) {
// Redirect to google.
$window.location.href = url;
});
};
$scope.signInGoogleCallback = function() {
return $http.get(backendConst.location + 'auth/google/callback', {
params: $location.search()
})
.success(function(user) {
$scope.gRes = 'Success!';
$location.search('code', null);
$location.hash(null);
AuthModel.setUser(user);
$location.path('/');
})
.error(function(err) {
$scope.gRes = 'Authentication failed, redirecting.';
$location.path('/authenticate');
});
为了更好的衡量,我的小/谷歌重定向页面是:
<div class="container clearfix" ng-init="signInGoogleCallback()">
<h1>Google YO!</h1>
<h4>{{gRes}}</h4>
</div>
所以发生了什么,它带来了“使用谷歌登录”页面,让我选择我的帐户,然后当它调用回调时,它会快速闪烁/谷歌视图,但然后踢我回到/验证,我开始的视图在 . 做一些调试表明
return $http.get(backendConst.location + 'auth/google/callback', {
params: $location.search()
})
即使在我的 endpoints 日志中,它表示/ auth / google / callback返回200.我不确定发生了什么 . 我知道这看起来不必要复杂但我用facebook和passport.js这样做,一切正常,除了我的 endpoints 日志显示用facebook参数“?code =”被添加到/ auth / facebook / callback url,就像我在我的控制器中的google回调函数中尝试使用{params:$ location.search()}一样 . 调试到 endpoints 显示它从未进入我离开// @ NEVER MAKES IT HERE注释的位置,但是当删除{params:$ location.search()}选项时,它使它成为passport.authenticate但不会超出 . 我完全难过了 . 我应该再次强调,我以完全相同的方式处理facebook身份验证,并且完美运行 .