2

完成教程并学习如何使用 Satellizer 对用户进行身份验证。刚刚完成了一个部分,我们从头开始构建了一个有效的谷歌身份验证服务。现在尝试使用 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 函数以将 $authProvider 与 Satellizer 一起使用时,即使我的 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);
};

});

应用程序.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'
    });
4

1 回答 1

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');
于 2015-07-04T01:05:08.137 回答