Hooks: Add request event handler for authentication

This commit is contained in:
2024-12-12 04:39:51 +01:00
parent a7b2bfb56b
commit dd1e6ee6c1
2 changed files with 62 additions and 0 deletions

View File

@ -0,0 +1,19 @@
import type { LayoutServerLoad } from "./$types";
// On each page load (every route), this function runs serverside.
// The "locals.user" object is only available on the server,
// since it's populated inside hooks.server.ts.
// It will populate the "user" attribute of each page's "data" object,
// so each page has access to the current user (or knows if no one is signed in).
export const load: LayoutServerLoad = ({ locals }) => {
if (locals.user) {
return {
user: locals.user,
admin: locals.user.admin,
};
}
return {
user: undefined,
};
};