Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect with basic auth to another website (in node)

How to redirect to another website with basic auth (in node)

Here is my code

const headers = {
  Authorization: "Basic " + 
  new Buffer(USER + ":" + PASS).toString("base64")
};

ctx.response.set(headers);
ctx.response.redirect(URL)

The First Response return with basic auth

Authorization →Basic QWRxXxXxYWRtaW4=
Connection →keep-alive
Content-Length →111
Content-Type →text/html; charset=utf-8
Date →Fri, 15 Dec 2017 21:49:57 GMT
Location →http://localhost:8080/edit/data/P0000013

The following redirected GET request doesn't contain basic auth and got redirected again, to a log-in page.

# General
Request URL:http://localhost:8080/edit/data/P0000013
Request Method:GET
Status Code:302 Found
Remote Address:[::1]:8080
Referrer Policy:no-referrer-when-downgrade

# Request Header
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Encoding:gzip, deflate, br
Accept-Language:en-US,en;q=0.9
Connection:keep-alive
Cookie:....
Host:localhost:8080
Upgrade-Insecure-Requests:1
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36
like image 293
Denly Avatar asked Jun 07 '26 15:06

Denly


1 Answers

  1. You CANNOT redirect with attached headers (including basic auth), HTTP protocol doesn't support it.

  2. But you can put your basic auth key/value pair in the new url as an argument:

    HTTP/1.x 302 Found
    Location: /api?auth=asdf

  3. Or save it in cookies

    HTTP/1.x 302 Found
    Location: /api
    Set-Cookie: auth=asdf

like image 104
Henry Leu Avatar answered Jun 10 '26 07:06

Henry Leu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!