Next.js Discord

Discord Forum

How do you get authenticated when calling an API Route from a Server Component

Answered
Picardy Spaniel posted this in #help-forum
Open in Discord
Picardy SpanielOP
I am using Next.js 14, and next-auth v5, looking at the documentation, it has some simplification, but everytime I try to call an API route from the Server Component, it throws the unauthorized error. How to properly use this next-auth v5 now?
Answered by joulev
Don’t fetch your own api routes in server components https://nextjs-faq.com/fetch-api-in-rsc
View full answer

7 Replies

Answer
@joulev Don’t fetch your own api routes in server components https://nextjs-faq.com/fetch-api-in-rsc
Picardy SpanielOP
Is it then recommended to make the app/some-page/page.tsx a client component if that's the case?
@Picardy Spaniel Is it then recommended to make the app/some-page/page.tsx a client component if that's the case?
No. It’s recommended that you put the logic in your route handler inside the server component
The component is your route handler
@joulev No. It’s recommended that you put the logic in your route handler inside the server component
Picardy SpanielOP
I don't get it, since you said that don't fetch own API Routes in Server Components, but put the logic in the Route Handler (which is the API Route), how can I call that route handler so that the server component will make use of it
Picardy SpanielOP
I see, alright, got it