2017-02-20 53 views
0

我有一個配置了端口3000上運行的Passport的環回API服務器。我的UI由來自端口3080的react JS組成。我想用Google實現身份驗證。對於這一點,從我反應過來的應用程序,我想提出一個呼叫 localhost:3000/auth/google 但這一呼聲沒有得到重定向因爲CORS錯誤的谷歌:在StrongLoop支持的ReactJS應用程序上啓用CORS

Fetch API cannot load http://localhost:3000/auth/google. Redirect from 'http://localhost:3000/auth/google' to 'https://accounts.google.com/o/oauth2/auth?response_type=code&redirect_uri=h…d=584602753475-1kk345gd8trcn31pbg1v2gncajhpakk8.apps.googleusercontent.com' has been blocked by CORS policy: Request requires preflight, which is disallowed to follow cross-origin redirect.

當我打http://localhost:3000/auth/google URL從一個新的瀏覽器標籤,一切工作正常。

任何人都可以告訴我應該在哪裏啓用cors?

編輯

我能看到的是,瀏覽器正在一個選項請求到localhost:3000/auth /中谷歌

這是我得到的時候我做了一個選項,捲曲要求: HTTP/1.1 204 No Content X-Powered-By: Express Vary: Origin Access-Control-Allow-Credentials: true Access-Control-Allow-Methods: GET,HEAD,PUT,PATCH,POST,DELETE Access-Control-Max-Age: 86400 Date: Mon, 20 Feb 2017 14:05:53 GMT Connection: keep-alive

回答

0

我不知道如何啓用Strongloop或ReactJS CORS,但來啓用它,你需要兩樣東西:

1. The right configuration of the JS request like so: 

var params = { 
    … 
    mode: 'cors', 
    credentials: 'include' //<- that gave me some headache a while ago, 
         //but is probably not necessary in your case. 
    … 
} 

fetch(new Request(<url>, params).then(res => …); 
  1. 正確的服務器配置。 Cors請求將首先使用預檢請求調用服務器,該請求會告訴瀏覽器是否可以訪問此資源,包括所有標題。此請求使用http OPTIONS方法/動詞,以便您的服務器需要正確響應。如果確實如此,瀏覽器將執行您想要執行的實際請求。

我希望有所幫助。

+0

感謝您的回覆。我在我的JS獲取請求中有這部分。瀏覽器向「http:// localhost:3000/auth/google」發出一個選項請求。這是事情被阻止的地方。 –

+0

您測試了選項請求嗎? http://stackoverflow.com/questions/14481850/how-to-send-a-http-options-request-from-the-command-line – philipp

+0

我嘗試了一個選項請求。響應是:'HTTP/1.1 204無內容 X-Powered-By:Express Vary:Origin Access-Control-Allow-Credentials:true Access-Control-Allow-Methods:GET,HEAD,PUT,PATCH,POST ,刪除 訪問控制最大年齡:86400 日期:2017年2月20日星期一14:05:53 GMT 連接:保持活動狀態 –