You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: apps/svelte.dev/content/docs/kit/20-core-concepts/20-load.md
+68Lines changed: 68 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -714,6 +714,74 @@ To prevent data waterfalls and preserve layout `load` caches:
714
714
715
715
Putting an auth guard in `+layout.server.js` requires all child pages to call `await parent()` before protected code. Unless every child page depends on returned data from `await parent()`, the other options will be more performant.
716
716
717
+
## Using `getRequestEvent`
718
+
719
+
When running server `load` functions, the `event` object passed to the function as an argument can also be retrieved with [`getRequestEvent`]($app-server#getRequestEvent). This allows shared logic (such as authentication guards) to access information about the current request without it needing to be passed around.
720
+
721
+
For example, you might have a function that requires users to be logged in, and redirects them to `/login` if not:
722
+
723
+
```js
724
+
/// file: src/lib/server/auth.js
725
+
// @filename: ambient.d.ts
726
+
interface User {
727
+
name: string;
728
+
}
729
+
730
+
declare namespace App {
731
+
interface Locals {
732
+
user?: User;
733
+
}
734
+
}
735
+
736
+
// @filename: index.ts
737
+
// ---cut---
738
+
import { redirect } from'@sveltejs/kit';
739
+
import { getRequestEvent } from'$app/server';
740
+
741
+
exportfunctionrequireLogin() {
742
+
const { locals, url } =getRequestEvent();
743
+
744
+
// assume `locals.user` is populated in `handle`
745
+
if (!locals.user) {
746
+
constredirectTo=url.pathname+url.search;
747
+
constparams=newURLSearchParams({ redirectTo });
748
+
749
+
redirect(307, `/login?${params}`);
750
+
}
751
+
752
+
returnlocals.user;
753
+
}
754
+
```
755
+
756
+
Now, you can call `requireLogin` in any `load` function (or [form action](form-actions), for example) to guarantee that the user is logged in:
757
+
758
+
```js
759
+
/// file: +page.server.js
760
+
// @filename: ambient.d.ts
761
+
762
+
declare module'$lib/server/auth' {
763
+
interface User {
764
+
name: string;
765
+
}
766
+
767
+
exportfunctionrequireLogin(): User;
768
+
}
769
+
770
+
// @filename: index.ts
771
+
// ---cut---
772
+
import { requireLogin } from'$lib/server/auth';
773
+
774
+
exportfunctionload() {
775
+
constuser=requireLogin();
776
+
777
+
// `user` is guaranteed to be a user object here, because otherwise
778
+
// `requireLogin` would throw a redirect and we wouldn't get here
Returns the current `RequestEvent`. Can be used inside `handle`, `load` and actions (and functions called by them).
22
+
23
+
In environments without [`AsyncLocalStorage`](https://nodejs.org/api/async_context.html#class-asynclocalstorage), this must be called synchronously (i.e. not after an `await`).
0 commit comments