Next.js Discord

Discord Forum

How do I check if the user is currently authorized or not in client?

Answered
Arcra posted this in #help-forum
Open in Discord
I want to show a logout button if the user is logged in.
I'm using app router and I could not make the component a server component. calling auth() in useEffect gives me error.
Answered by B33fb0n3
normally auth libraries also provide ways on how to get the session clientside. If that does not exists, you can pass the session from server to clientside and render it there. Or, even better: you can use SSR to either render a login button or render a log out button.

TL;DR:
1. use a hook to get the session clientside
2. pass data from server to client via props
3. use SSR to render the correct button
View full answer

6 Replies

@Arcra I want to show a logout button if the user is logged in. I'm using app router and I could not make the component a server component. calling `auth()` in `useEffect` gives me error.
normally auth libraries also provide ways on how to get the session clientside. If that does not exists, you can pass the session from server to clientside and render it there. Or, even better: you can use SSR to either render a login button or render a log out button.

TL;DR:
1. use a hook to get the session clientside
2. pass data from server to client via props
3. use SSR to render the correct button
Answer
In Auth.js [docs](https://authjs.dev/getting-started/session-management/get-session), I couldn't find anything about app router. I think it is not good to SessionProvider
@Arcra solved?
I just used SessionProvider which is not recommended, thanks for help
@Arcra I just used SessionProvider which is not recommended, thanks for help
thanks for clarifying that you chose 1.
Happy to help