Next.js Discord

Discord Forum

How to implement custom api authentication in Next 14 app router

Answered
White Ibis posted this in #help-forum
Open in Discord
White IbisOP
I have a custom api that has its own authentication service which acts like,
- it gives access_token and refresh_token pair in login
- every 5 mins or so our access_token expires, then we have to hit api with previous refresh token to get new pair of access and refresh token
How can I achieve that in app router, Can you give me a high level overview of that.
Answered by B33fb0n3
Amplify auth places the tokens in your cookies. So also your website cookies. So you can access them serverside
View full answer

7 Replies

For me aws amplify auth manages the auth states. And exactly like you told: give a pair of tokens and refresh them after time. I send mine with the request that I do. If the client uses an expired accesstoken, the endpoint will automatically create and set new. Like that you have everytime a valid token and don't need to worry about token refresh. That works fine for me 🙂 @White Ibis
White IbisOP
Sure, but I'm curious about AWS Amplify Auth—does it place tokens in cookies or include them in the response body? If it uses cookies, there might be an issue using them with the backend since these cookies are linked to the AWS domain (I plan to switch to cookie authentication in the future). On the other hand, if it provides tokens in the response body, how would you go about authenticating with your server during server-side rendering (SSR)?

I've explored Next Auth, which is an excellent framework for managing authentication in Next.js. However, I'm still trying to understand how it seamlessly integrates with our custom auth API.
White IbisOP
I am concerend about ssr and react server components
Amplify auth places the tokens in your cookies. So also your website cookies. So you can access them serverside
Answer
White IbisOP
ok thanks for help, I read about cookie in same origin, as in same origin backend can access cookies assigned by another domain.