siva
siva

Reputation: 1523

Nextjs 13 authentication between server component and separate backend

I am unsure about how to make API calls from Nextjs 13 to my separate Python backend.

I have two scenarios:

The client side component has the correct session cookie from my auth provider (Clerk). So that case is pretty straight forward and I can authenticate the call in the Python backend.

However, when the call comes from the server side session cookies are not included. I could send a JWT token along. But that would add quite a bit of complexity.

What approach would you recommend in that scenario?

Upvotes: 4

Views: 1165

Answers (1)

Abdul Kabeer
Abdul Kabeer

Reputation: 287

I have the same doubt. Not sure how to approch this. In my case the seperate backend can either send jwt token in response or as cookie. i want to get user data in server and send it to brower, to prevent flash screens.

one possible solution

When the external server sends jwt token ,Nextjs cant store it anywhere. it has to be maintained in user browser. so the flow would be

Request

user browser (login page) -> nextjs API -> External Server (Login route)

Response

external server (JWT TOKEN) -> nextJS API (SET cookie in user browser) -> user browser

from now on when ever you request to any page , you have cookie available to access in next server component , then make request to external server apis using the cookie

let me know if there is a better approch

Upvotes: 0

Related Questions