Next.js Discord

Discord Forum

Get the auth user data in the server side

Unanswered
Barbary Lion posted this in #help-forum
Open in Discord
Barbary LionOP
I have built a hook that get the user data in the client side and it works by just fetching the /api/user endpoint (i am using laravel as a backend) but when trying to get the user data in the server side in nextjs by fetching the same endpoint it didnt't works and i got 401 response from laravel, is there any solution ??

6 Replies

Barbary LionOP
Waiting for your help guys
Barbary LionOP
Still getting 401 response
@Barbary Lion Still getting 401 response
can you check which cookies are there on your backend (the one that you are requesting the data from)
Barbary LionOP
i tryed to get all the headers from the client request and send them with the request to get the data and it works only if he is authenticated, if not i got a 500 error in nextjs
with this error in the console Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
1. You might have mismatching versions of React and the renderer (such as React DOM)
2. You might be breaking the Rules of Hooks
3. You might have more than one copy of React in the same app
See https://react.dev/link/invalid- debug and fix this problem.
⨯ [TypeError: Cannot read properties of null (reading 'useContext')] {
digest: '2199604760'
}