SvelteKit - Login/Logout in a Navbar component #447
-
I'm currently working on a SvelteKit project with Lucia and Prisma and I was wondering if anyone had suggestions on how I could conditionally render the correct button in my Navbar component (i.e., render a logout button when the user is logged in and vice versa) I was also wondering where the best place would be to handle the logout logic? I already have a +page.svelte and +page.server.ts file for my login page that handles that logic -- would it be a good idea to handle to logout logic in my Navbar.svelte file or somewhere else like my api directory? Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
You can get the current session in It seems you can't handle form actions inside |
Beta Was this translation helpful? Give feedback.
You can get the current session in
+layout/page.server.ts
and pass it to your nav bar.It seems you can't handle form actions inside
+layout.server.ts
. If you want to have the logout button inside the nabber, you may be able to create/logout/+page.server.ts
and exportactions
that handle sign out there. Then, you can create a form withuse:action
whereaction="/logout"
inside the nav bar. This will make sure the logout button works for both JS and non-JS users.