Newbie question: How do we save post login user into event.locals when using kinde with sveltekit?
HI, I am trying to use Kinde with my existing sveltekit project and wondering how are people using event.locals to save loggedin user and token. I can't seem to find in docs any example of the same? are people checking for isAuthenticated on every route?
4 Replies
In SvelteKit projects using Kinde, the typical approach to handle authentication and manage user sessions involves using the
Accessing event.locals in Routes: In any specific route, you can then access event.locals.user and event.locals.token to get the user information and token, respectively. Check isAuthenticated on Necessary Routes: It's common to check isAuthenticated in routes where user authentication is required. This can be done directly in the route's server-side logic or in load functions in +page.server.ts files. This approach ensures that you're checking the authentication status and managing user sessions effectively.
sessionHooks
from the SDK.
Modify hooks.server.ts: In your src/hooks.server.ts, you can use sessionHooks to manage the session. Here, you can assign the user and token to event.locals after checking authentication status. This might look something like:
Accessing event.locals in Routes: In any specific route, you can then access event.locals.user and event.locals.token to get the user information and token, respectively. Check isAuthenticated on Necessary Routes: It's common to check isAuthenticated in routes where user authentication is required. This can be done directly in the route's server-side logic or in load functions in +page.server.ts files. This approach ensures that you're checking the authentication status and managing user sessions effectively.
this does not work... not matter how many times I sign out and sign in ...at this point the event.locals is empty object but if I check for on +page.server or +layout.server ... its value is true.
also I would like to check for private routes in hooks.server rather than individual pages ... too much duplication of code
the package version I am using is
any suggestions?
Apoligies for the delay in a reply, I will check into this for you
Hi @jasdeep , apologies no one has come back to you, I will get this followed up.
Is this still an issue you're facing?
Is this still an issue you're facing?
@Daniel_Kinde I have worked around the issue for now but not in a clean way as I mentioned .... checking if route is authenticated in one place rather than repeating the same code across all pages