Authentication header not reaching server in React & NodeJS app after deploying on server

I have an app with front end built in React and backend with NodeJS. My app is working fine when I run locally on my computer using local hosts. However, when I deployed my app on render.com, I’m facing an issue with my authentication header.

I login to app successfully, but after that to access APIs I have to use JWT. I receive the JWT after login and include it in my request to later APIs, but my server do not receive the authentication header. Following snapshot shows the problem:

  1. Request sent from chrome includes authorization header.

However, on server side following header is received:

  1. Response received from server shows there was no header in request.

I’ve attached both screenshots from chrome developer options. Also below you can see snapshot for console for header received on server:

Reached verifyToken with token: {“host”:”nearusnewadminserver.onrender.com”,”user-agent”:”Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36″,”accept”:”/“,”accept-encoding”:”gzip, br”,”accept-language”:”en-US,en;q=0.9,ur-PK;q=0.8,ur;q=0.7,fa-IR;q=0.6,fa;q=0.5,ar-AE;q=0.4,ar;q=0.3″,”access-control-request-headers”:”authorization”,”access-control-request-method”:”GET”,”cdn-loop”:”cloudflare; loops=1; subreqs=1″,”cf-connecting-ip”:”185.67.127.180″,”cf-ew-via”:”15″,”cf-ipcountry”:”GB”,”cf-ray”:”8be10f90528977a5-LHR”,”cf-visitor”:”{“scheme”:”https”}”,”cf-worker”:”onrender.com”,”origin”:”http://localhost:8000″,”priority”:”u=1, i”,”referer”:”http://localhost:8000/”,”render-proxy-ttl”:”4″,”rndr-id”:”d77239b2-95cc-481b”,”sec-fetch-dest”:”empty”,”sec-fetch-mode”:”cors”,”sec-fetch-site”:”cross-site”,”true-client-ip”:”185.67.127.180″,”x-forwarded-for”:”185.67.127.180, 10.222.42.14, 10.223.245.2″,”x-forwarded-proto”:”https”,”x-request-start”:”1725485217438628″}

Note that I had to add following middleware to my backend as my front end host and backend host are different on render.com: (In below snapshot I used my dev-server host url as I’m testing with it to make sure header is included.

function accessControl(req,res,next){
    console.log(`Reached access control allow part`);
    res.setHeader("Access-Control-Allow-Origin", "http://localhost:8000");
    res.header(
      "Access-Control-Allow-Headers",
      "Origin, X-Requested-With, Content-Type, Accept, Authorization"
    );
    next();
}

module.exports = {
    accessControl
}

I have searched many forums but couldn’t find the solution. It is also worth noting that when I send the same request with Postman it works fine.

It looks like you have a CORS error in the browser console because the options request failed.

Please check how it’s implemented in cors lib
https://github.com/expressjs/cors/blob/master/lib/index.js#L159
Firstly you need to handle Options request and send successful response. You forgot about Access-Control-Allow-Methods.

I recommend you to use this lib instead of yours uncompleted solution https://www.npmjs.com/package/cors
Example:

const express = require('express')
const cors = require('cors')
const app = express()
 
app.use(cors({
  origin: ["http://localhost:8000"],
}))
 
app.get('/countries', function (req, res, next) {
  res.json(['US']);
})
 
app.listen(80, function () {
  console.log('server listening')
})

Provide the code how you are using this accessControl middleware.
For this to work, it should look like this:

app.get('/countries', accessControl, (req, res) => {
  res.json(['US']);
});

or like this:

app.use(accessControl)

app.get('/countries', (req, res) => {
  res.json(['US']);
});

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật