Nextjs auth and interceptors
Answered
Oriental Scops-Owl posted this in #help-forum
Oriental Scops-OwlOP
Hi i am migrating over to nextjs 14. all of my react apps and my previous next app uses axios and interceptors to handle authentication and retry policy.
i tried implementing the same class inside of nextjs 14 inside server action file as i am trying to handle all of the auth calls and API alls on the server-side now, rather than client side.
I am receiving the following error in screenshot. it seems i can actually initialize a class within a server action.
any idea on how i can rewrite this? or is there a way natively to handle auth/cookies on server-side with an interceptor and retry policy?
i tried implementing the same class inside of nextjs 14 inside server action file as i am trying to handle all of the auth calls and API alls on the server-side now, rather than client side.
I am receiving the following error in screenshot. it seems i can actually initialize a class within a server action.
any idea on how i can rewrite this? or is there a way natively to handle auth/cookies on server-side with an interceptor and retry policy?
Answered by joulev
now i dont know what your architecture is, but the error is pretty clear and very simple to fix. just move the class to a different file not marked with
use server, then import it to the use server file5 Replies
@Oriental Scops-Owl Hi i am migrating over to nextjs 14. all of my react apps and my previous next app uses axios and interceptors to handle authentication and retry policy.
i tried implementing the same class inside of nextjs 14 inside server action file as i am trying to handle all of the auth calls and API alls on the server-side now, rather than client side.
I am receiving the following error in screenshot. it seems i can actually initialize a class within a server action.
any idea on how i can rewrite this? or is there a way natively to handle auth/cookies on server-side with an interceptor and retry policy?
now i dont know what your architecture is, but the error is pretty clear and very simple to fix. just move the class to a different file not marked with
use server, then import it to the use server fileAnswer
you cannot export anything except async functions from
use server filesOriental Scops-OwlOP
gotcha. alright. the issue i have is inside of some of those class functions, it is setting cookies which require the 'use server' line
you can still move it out of the
use server file i think. just ensure that you only use the class and run those functions inside a function exported from use serverOriental Scops-OwlOP
gotcha so makea wrapper basically