Unauthorized error when calling an API Route from a Server Component
Unanswered
Picardy Spaniel posted this in #help-forum
Picardy SpanielOP
I am using next-auth, currently my app/profile/page.tsx has this snippet attached, as well as my app/api/profile/route.ts (imports the getUserProfile.ts)
I have included the bearer token already, but it still fails as unauthorized, any additional setup that I might have missed?
I have included the bearer token already, but it still fails as unauthorized, any additional setup that I might have missed?
2 Replies
@Picardy Spaniel I am using next-auth, currently my app/profile/page.tsx has this snippet attached, as well as my app/api/profile/route.ts (imports the getUserProfile.ts)
I have included the bearer token already, but it still fails as unauthorized, any additional setup that I might have missed?
Try this instead
import { headers } from ‘next/headers’
fetch(url, {
headers: new Headers(headers())
}
import { headers } from ‘next/headers’
fetch(url, {
headers: new Headers(headers())
}
Picardy SpanielOP
Thank you, I already got it working by getting the cookies, and setting it on the header.cookie