卫星google auth服务 - CORS问题

时间:2015-07-02 21:38:22

标签: angularjs authentication satellizer

完成教程并学习如何使用Satellizer对用户进行身份验证。我刚刚完成了一个部分,我们从头开始构建了一个google auth服务。现在尝试使用Satellizer模块执行相同的操作但我现在收到此错误:

  

XMLHttpRequest无法加载http://localhost:3000/auth/google。凭证>标志为真时,不能在'Access-Control-Allow-Origin'标头中使用通配符'*'>。因此,不允许原点“http://localhost:9000”访问。

以下是我的设置:是设置标题的中间件:

    app.use(function (req, res, next){
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
    res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization');

    next();
});

我不明白的是我的控制器中的$ scope.google函数可以工作,但当我切换到$ scope.authenticate函数以使用带有Satellizer的$ authProvider时,即使我的Access头也出现了错误不要改变。知道我做错了吗?

我的HTML:

<button ng-click="authenticate('google')" class="btn btn-lg btn-default btn-block" type="button">Google</button>

控制器:

angular.module('psJwtApp').controller('LoginCtrl', function ($scope, alert, auth, $auth) {
$scope.submit = function(){

    auth.login($scope.email, $scope.password)
        .success(function(res){
            alert('success', 'Welcome', 'Thanks for coming back, ' + res.user.email + '!');
        })
        .error(handleError);
};

$scope.google = function() {
    auth.googleAuth().then(function(res){
        alert('success', 'Welcome', 'Thanks for coming back, ' + res.user.displayName + '!');
    }, handleError);
};

function handleError(err) {
    alert('warning', 'Something went wrong :(', err.message);
}

$scope.authenticate = function(provider) {
    $auth.authenticate(provider).then(function(res){
        alert('success', 'Welcome', 'Thanks for coming back ' + res.data.user.displayName + '!');
    }, handleError);
};

});

app.js:

angular.module('psJwtApp', ['ui.router', 'ngAnimate', 'satellizer']);

app.config.js:

   angular.module('psJwtApp').config(function($stateProvider, $urlRouterProvider, $httpProvider, $authProvider, API_URL){
...
$authProvider.google({
        clientId: 'clientId',
        url: API_URL + 'auth/google'
    });

1 个答案:

答案 0 :(得分:1)

好吧,2天后我终于解决了。不确定它为什么有效。

将我的CORS设置更改为:

res.header('Access-Control-Allow-Origin', 'http://localhost:9000');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
res.header('Access-Control-Allow-Credentials', 'true');
相关问题