K
Kinde7mo ago
jasdeep

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
onderay
onderay7mo ago
In SvelteKit projects using Kinde, the typical approach to handle authentication and manage user sessions involves using the 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:
import { sessionHooks, type Handler } from '@kinde-oss/kinde-auth-sveltekit';

export const handle: Handler = async ({ event, resolve }) => {
sessionHooks({ event });
const response = await resolve(event);
if (event.locals.isAuthenticated) {
event.locals.user = await event.locals.getUser();
event.locals.token = await event.locals.getToken();
}
return response;
};
import { sessionHooks, type Handler } from '@kinde-oss/kinde-auth-sveltekit';

export const handle: Handler = async ({ event, resolve }) => {
sessionHooks({ event });
const response = await resolve(event);
if (event.locals.isAuthenticated) {
event.locals.user = await event.locals.getUser();
event.locals.token = await event.locals.getToken();
}
return response;
};

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.
jasdeep
jasdeepOP7mo ago
if (event.locals.isAuthenticated) {
event.locals.user = await event.locals.getUser();
event.locals.token = await event.locals.getToken();
}
if (event.locals.isAuthenticated) {
event.locals.user = await event.locals.getUser();
event.locals.token = await event.locals.getToken();
}
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
const isAuthenticated = await kindeAuthClient.isAuthenticated(
request as unknown as SessionManager
);
const isAuthenticated = await kindeAuthClient.isAuthenticated(
request as unknown as SessionManager
);
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
"@kinde-oss/kinde-auth-sveltekit": "^1.3.0"
"@sveltejs/kit": "^2.5.6",
"@kinde-oss/kinde-auth-sveltekit": "^1.3.0"
"@sveltejs/kit": "^2.5.6",
any suggestions?
Daniel_Kinde
Daniel_Kinde5mo ago
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?
jasdeep
jasdeepOP5mo ago
@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
Want results from more Discord servers?
Add your server