Accessing NextRequest in a Server Action
Answered
Schweizerischer Niederlaufhund posted this in #help-forum
Schweizerischer NiederlaufhundOP
I'd like to check if the currentUser is authenticated when a server action is triggered. The docs suggest this design pattern (https://nextjs.org/docs/app/building-your-application/data-fetching/server-actions-and-mutations#authentication-and-authorization) but most auth libraries (like next-auth) require you to pass in a the request object to parse out the session/jwt information. How can I access the request object in a Server Action? Or is this not supported and my only option is to create an API endpoint for this work?
Thanks!
Thanks!
Answered by Ray
anyway, you can always access the header and cookies with
header() or cookie() function4 Replies
@Schweizerischer Niederlaufhund I'd like to check if the currentUser is authenticated when a server action is triggered. The docs suggest this design pattern (https://nextjs.org/docs/app/building-your-application/data-fetching/server-actions-and-mutations#authentication-and-authorization) but most auth libraries (like next-auth) require you to pass in a the request object to parse out the session/jwt information. How can I access the request object in a Server Action? Or is this not supported and my only option is to create an API endpoint for this work?
Thanks!
next-auth can do this
https://next-auth.js.org/configuration/nextjs#in-app-router
https://next-auth.js.org/configuration/nextjs#in-app-router
Schweizerischer NiederlaufhundOP
Interesting, the issue might just be with thirdweb auth https://github.com/thirdweb-dev/js/tree/main/packages/auth
what is this?
anyway, you can always access the header and cookies with
header() or cookie() functionAnswer