reactjs - how to fetch headers in middleware in Next js - Stack Overflow
In below NextJS code how can i access Authorization in middleware when client component call API every time api is call it show empty headers in middleware can some on explain it
Client Component:-
'use client'
import { useEffect } from "react";
export default function Page(){
useEffect(()=>{
const data = async () => {
const log = localStorage.getItem("login");
let usr = await fetch("http://localhost:3000/api/route",{
method: "GET",
headers:{
"Content-Type": "application/json",
"Authorization": log
}
});
usr = await usr.json();
console.log(usr);
}
data();
},[]);
return(
<div>
<h1>Login Middleware</h1>
</div>
);
}
API Route:-
import { NextResponse } from "next/server";
export function GET(request){
return NextResponse.json({msg: "Api Call"});
}
Middleware :-
const { NextResponse } = require("next/server");
export function middleware(request){
const head = request.headers;
console.log(head);
}
export const config = {
matcher: ["/login"],
}
In below NextJS code how can i access Authorization in middleware when client component call API every time api is call it show empty headers in middleware can some on explain it
Client Component:-
'use client'
import { useEffect } from "react";
export default function Page(){
useEffect(()=>{
const data = async () => {
const log = localStorage.getItem("login");
let usr = await fetch("http://localhost:3000/api/route",{
method: "GET",
headers:{
"Content-Type": "application/json",
"Authorization": log
}
});
usr = await usr.json();
console.log(usr);
}
data();
},[]);
return(
<div>
<h1>Login Middleware</h1>
</div>
);
}
API Route:-
import { NextResponse } from "next/server";
export function GET(request){
return NextResponse.json({msg: "Api Call"});
}
Middleware :-
const { NextResponse } = require("next/server");
export function middleware(request){
const head = request.headers;
console.log(head);
}
export const config = {
matcher: ["/login"],
}
Share
Improve this question
asked 23 hours ago
GAMING WARRIORGAMING WARRIOR
91 silver badge1 bronze badge
1 Answer
Reset to default 0You can access the authorization at your middleware like this :
const authHeader = request.headers.get("authorization");
console.log("Authorization Header:", authHeader);
最新文章
- 拥抱安卓的诺基亚能否逆势崛起?
- 微软每年从安卓厂商获20亿美元专利费
- 微软发布Surface平板:定义并示范Win8用户体验
- Windows8是备胎?解析微软移动市场战略
- opengl - How to render a FBO into an ImGui window? - Stack Overflow
- r markdown - Rstudio custom traceback - Stack Overflow
- Unable to deploy flex consumption function app using azure bicep - Stack Overflow
- Google Translate Widget Only Works in Chrome – Why? - Stack Overflow
- soa - How to use a variable in a composite or wsdl, as I need to have this variable per environment? - Stack Overflow
- python - ConnectionError(err, request=request) requests.exceptions.ConnectionError: ('Connection aborted.', Remo
- rust - Basic bracket-lib example crashes with “unsafe precondition(s) violated: slice::from_raw_parts” - Stack Overflow
- monit missing file in sys directory when restarting monit - Stack Overflow
- Laravel Livewire Pagination is not responsive - Stack Overflow
- python - How can I use FilteredSelectMultiple widget in a django custom form? - Stack Overflow
- sql - Merge tables with different timestamps? - Stack Overflow
- Composing dependent functions in Lean - Stack Overflow
- Disabling a JavaScript function when the device is offline - Stack Overflow