简易节点身份验证:谷歌



我正试图用谷歌登录我的应用程序,但它显示了一个错误,如下所示,

XMLHttpRequest cannot load https://accounts.google.com/o/oauth2/v2/auth?response_type=code&redirect_ur…d=410427634474-u3tpasmj4r80s6v20o54s85fikhotl79.apps.googleusercontent.com. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

我试过类似以下的东西

app.js

passport.use(new GoogleStrategy({
    clientID        : config.googleAuth.clientID,
    clientSecret    : config.googleAuth.clientSecret,
    callbackURL     : config.googleAuth.callbackURL
},
function(accessToken, refreshToken, profile, done) {
   User.findOrCreate({ googleId: profile.id }, function (err, user) {
     return done(err, user);
   });
}
));

router.get('/pages/auth/loginWithGoogle',
    passport.authenticate('google', { scope: ['https://www.googleapis.com/auth/plus.login'] }));
router.get('/auth/google/callback',
passport.authenticate('google', {
   successRedirect : '/invite-friends',
   failureRedirect : '/pages/auth/login'
}));

$http()它将发送一个ajax请求,这将不起的作用

下面的代码在一个新窗口中打开url,然后如果用户授权并且一切正常,它将把主页重定向到/invite-friends

    vm.loginWithGoogle = function(){
        var win = window.open('/api/pages/auth/loginWithGoogle', "windowname1", 'width=800, height=600'); 
        var REDIRECT = 'callback';
        var pollTimer   =   window.setInterval(function() { 
            try {
                if (win.document.URL.indexOf(REDIRECT) != -1) {
                    window.clearInterval(pollTimer);
                    window.location = window.location.origin + '/invite-friends';
                    win.close();
                }
            } catch(e) {
            }
        }, 100);
    }

或者使用它在当前页面中打开url:

vm.loginWithGoogle = function(){
    location.href = '/api/pages/auth/loginWithGoogle';
}

最新更新