After login is successful, how to pass id information on to other pages?
Answered
Giant Chinchilla posted this in #help-forum
Giant ChinchillaOP
I have a Spring Boot application in the backend and I use next.js in the frontend with React.
I make a POST request to the backend to login and if the login is successful it returns a body with the user id, user name and their role.
Based on their role I redirect them to the correct page, but I also need to pass their id to hit the correct endpoints to show them their correct data.
Searching documentation I found some ways this can be achieved in Next.js, namely:
* useParams or useSearchParams: the id is passed through url, but this can be easily exploited by changing the id
* useLocalStorage, storing the id locally, but I'm not sure how safe this is either
Does anyone know the most common way this is done? Passing sensitive information in between pages? After a login, for instance?
Is this the kind of situation where NextAuth.js would shine? I only discovered it today, will read up on it later
I make a POST request to the backend to login and if the login is successful it returns a body with the user id, user name and their role.
Based on their role I redirect them to the correct page, but I also need to pass their id to hit the correct endpoints to show them their correct data.
Searching documentation I found some ways this can be achieved in Next.js, namely:
* useParams or useSearchParams: the id is passed through url, but this can be easily exploited by changing the id
* useLocalStorage, storing the id locally, but I'm not sure how safe this is either
Does anyone know the most common way this is done? Passing sensitive information in between pages? After a login, for instance?
Is this the kind of situation where NextAuth.js would shine? I only discovered it today, will read up on it later
Answered by Bumble bee
To persist user data on your frontend you can use react context hook and create an auth provider like this example: https://calebbenjin.hashnode.dev/http-only-cookies-authentication-with-nextjs
Next-auth uses server side sessions. If your backend already manages sessions or jwt, you don't really need next-auth
Next-auth uses server side sessions. If your backend already manages sessions or jwt, you don't really need next-auth
1 Reply
Bumble bee
To persist user data on your frontend you can use react context hook and create an auth provider like this example: https://calebbenjin.hashnode.dev/http-only-cookies-authentication-with-nextjs
Next-auth uses server side sessions. If your backend already manages sessions or jwt, you don't really need next-auth
Next-auth uses server side sessions. If your backend already manages sessions or jwt, you don't really need next-auth
Answer