簡體   English   中英

帶有Angular的Node Express Passport-不兼容?

[英]Node Express Passport with Angular - not compatible?

我正在一個簡單的Web應用程序上運行,該應用程序在服務器上使用Passport通過Google等進行身份驗證來運行Node / Express。客戶端正在使用Angular。

但是,我無法讓Angular與Passport配合使用以登錄Google。 我的設置如下:

節點/快速/護照REST端點:

app.get("/auth/google",
function(req, res, next) {
    passport = req._passport.instance;
    passport.authenticate("google", { scope: ['https://www.googleapis.com/auth/userinfo.profile', 'https://www.googleapis.com/auth/userinfo.email'] })(req, res, next);     

});

客戶端...

myModule.controller("myController", function($q, $scope, $http) {
$scope.loginWithGoogle = function() {
    $http.get("myDomain/auth/google").then(function(response) {
        doStuff();
    });
}

...由一個簡單的調用:

<button ng-click="loginWithGoogle ()">Login</button>

我收到的錯誤是“ Access-Control-Allow-Origin”標頭出現在請求的資源上。 因此,不允許訪問源“ myDomain”。

但是,如果我直接通過瀏覽器中的myDomain / auth / google訪問REST端點,則可以正常工作。 或者,我可以制作一個簡單的HTML <a href="/auth/google">Login</a>並使它也能正常工作。 因此,我猜測客戶端和服務器之間的連線出現了問題。

我一直在尋找解決方案,但找不到任何有效的方法。 例如,我讀到一個建議使用$ http.jsonp而不是$ http.get。 這產生了一些進展-如果用戶已經通過gmail,其他應用程序等登錄了Google,那么他們可以進行身份​​驗證。 但是,如果該用戶尚未登錄Google,則會收到錯誤消息'Uncaught SyntaxError:Unexppected token <' 我認為這是Google返回其登錄頁面的HTML。

有沒有辦法讓Angular在這里使用Node / Express / Passport玩得開心? 提前致謝!

即使在主文件中已處理了CORS標頭,也要在每次發送響應時對其進行設置。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM