2

This is my stack:

  1. Frontend server: Node + ReactJS (f.com)
  2. Backend server (API): Golang (b.com)

I'm doing this in development environment where I use create-react-app to run my frontend server via npm start command. I want to perform authentication using SSO served at sso.example.com/login. Ideally whenever there's a request to the Backend server without proper authorization, it will be responded with HTTP 302 Redirect to the SSO page. So the sequence is:

  1. ReactJS sends GET request to b.com/users
  2. Golang responded with http.Redirect("sso.example.com/login")
  3. ReactJS app gets CORS error from sso.example.com of which I don't have control of adding Access-Control-Allow-Origin:* respond header

How can I redirect my GET request successfully?

yonasstephen
  • 2,546
  • 3
  • 20
  • 46
  • Does your react app get the cors error response from `b.com` or from `sso.example.com`? – mkopriva Oct 01 '17 at 16:56
  • @mkopriva oh yeah I just added that into the question. it's from `sso.example.com` – yonasstephen Oct 01 '17 at 17:00
  • If `sso.example.com` does not allow cross origin requests and you don't have access to its server to modify the access controll then I'm not sure your react app will ever be able to access its resources. – mkopriva Oct 01 '17 at 17:09

1 Answers1

2

In your case, it would be easier to do the redirection in browser on the client side, so you won't have CORS issues (because you are accessing it from the SSO website url directly).

You can either do it with react, which is complicated: Programmatically navigate using react router V4

Or do it with window with plain JavaScript, which is easy but might cause problems with browsing history:

window.location.href = "https://www.example.com";

https://appendto.com/2016/04/javascript-redirect-how-to-redirect-a-web-page-with-javascript/

paradite
  • 5,641
  • 3
  • 35
  • 54
  • ahh okay, that's what I thought. I was just thinking if there's anything that I can do to avoid CORS on redirect in this scenario. thanks for the advice tho! – yonasstephen Oct 02 '17 at 05:40