0

My RestAPI server redirects a user request to another webpage (for example google.com) how can I redirect my fron-end to that page, I mean I have a button, when I press on it sends an axios request to my server. my server redirects it to another web page, and I want to show to my client that web-page

I have made an axios

 const url = "https://cors-anywhere.herokuapp.com/http://147.13.121.244:4001/redirect"
const client = axios.create({
  headers: {
    'Content-Type': 'application/json',
    'Authorization': 'Basic a3p0ZXN5547asdhMnFUdVZN',

  }
});

let result = await client.get(url, {}).then((response) => {
  return response
}).catch((error) => {
  return error
})
Akezhan
  • 73
  • 6

1 Answers1

0

You can use window.location to redirect the current page to the URL you receive from the server or window.open to open that page in another tab.

For example:

const redirectURL = // result.redirect
// Redirect
window.location = redirectURL;
// New Window
window.open(redirectURL, "_blank");

You'll have to examine the result object to figure out how exactly to get the redirect URL, but once you have it, these will work.

Henry Woody
  • 14,024
  • 7
  • 39
  • 56
  • the thing is I need to set Basic Auth before redirection if I will make window.location = result.url; it does not set header I guess – Akezhan Mar 19 '19 at 17:11
  • is the redirect location part of your app or on another host? – Henry Woody Mar 19 '19 at 17:14
  • Redirect will execute a new http request and your headers will be lost. – Hasta Tamang Mar 19 '19 at 17:14
  • @HastaTamang I have a url ("http://google.com") I want to move my client to that url and I need my user to have authorization in Header, how can I solve that – Akezhan Mar 19 '19 at 17:19
  • @HenryWoody another – Akezhan Mar 19 '19 at 17:23
  • It does not seem possible to set HTTP headers when redirecting client-side. Examples: [a](https://stackoverflow.com/questions/35236874/setting-custom-request-header-on-a-page-redirect), [b](https://stackoverflow.com/questions/35966526/add-header-to-window-location-pathname), [c](https://stackoverflow.com/questions/35966526/add-header-to-window-location-pathname), [d](https://stackoverflow.com/questions/15835783/adding-http-request-header-to-a-a-href-link) – Henry Woody Mar 19 '19 at 17:24
  • 1
    Use `query string` or `session` – Hasta Tamang Mar 19 '19 at 17:24
  • @HastaTamang, I need to authorize in another web-page not mine, is it possible to set session in my server, redirect to another web-page and authorize there with session? – Akezhan Mar 19 '19 at 17:27
  • Also how do you have access authorization headers to another host from your site? (seems like a possible security hazard) – Henry Woody Mar 19 '19 at 17:27
  • @HenryWoody, yeah I know, the thing is, this another page is a bank and I am making a platform where people can make loan orders, In the API which the bank gave us, there is a method which returns a URL if every credential is right, so they want us to redirect a client to the url and the client should be authorized (in client's header should be basic auth) and Idk how to do that, how to redirect with header – Akezhan Mar 19 '19 at 17:38
  • You should maybe look into the [OAuth2 protocol](https://oauth.net/2/). I don't think your application should receive someone else's bank credentials even if you just pass them along to the bank site. If you're developing for the bank and working with them, then maybe you can just use the query string and then let the bank take information from the query string (but the user would still need to log into the bank site separately) – Henry Woody Mar 19 '19 at 17:56