TheBinaryGuy / next-hono-lucia

A Next.js template that uses Hono as the API framework and Lucia for authentication.
MIT License
150 stars 7 forks source link

How to get user in client side? #4

Closed Supavasinan closed 2 weeks ago

TheBinaryGuy commented 3 weeks ago

Hey sorry, was a bit busy.

There are multiple ways to achieve this, if it's the immediate child that needs it, pass it as a prop, otherwise add file to your repo: components/auth-provider.tsx

Then wrap the pages that require auth with the AuthProvider, like shown:

https://github.com/TheBinaryGuy/next-hono-lucia/blob/28eecbc53acd18f293e69c72f616493105e0b0ec/src/app/page.tsx

If it's too much to wrap all pages with the auth provider, use a route group and add a layout file with the auth stuff.

And the all the children client components can access the user via the useAuth hook, like this.

https://github.com/TheBinaryGuy/next-hono-lucia/blob/28eecbc53acd18f293e69c72f616493105e0b0ec/src/app/client-user.tsx

Hope this helps, added this into the template as well :)

TheBinaryGuy commented 2 weeks ago

Hey @Supavasinan, I'm closing this issue now, if you feel like the above doesn't address your issue, feel free to reopen it :)