Next.js Discord

Discord Forum

token value is undefined in nextApiRequest

Answered
Dutch posted this in #help-forum
Open in Discord
DutchOP
Hi guys,
i need some help
nextApiRequest has headers with some keys but it returns undefined when i try to get a key
req.header
i want to get authorization but it comes undefined
i trid many ways to get it
const authorization = req.headers["authorization"];
const { authorization } = req.headers;
not working
Answered by Dutch
i have fixed it with this part

req.headers.get("authorization")?.replace("Bearer ", "");
View full answer

13 Replies

DutchOP
export async function createContext(opts: CreateNextContextOptions) {
const { req, res } = opts;

const token = req.headers.authorization?.replace("Bearer ", "");
  console.log("token", token);

// token is undefined
...
}
DutchOP
@alfonsüs ardani ?
@Oriental please help
@Dutch <@194128415954173952> ?
Have you tried the code i sent you
Is this pages dir?
What nextjs version are you using?
@Dutch please answer
DutchOP
@alfonsüs ardani
DutchOP
i have fixed it with this part

req.headers.get("authorization")?.replace("Bearer ", "");
Answer
DutchOP
i'm using app router