Skip to content

Commit 2c961b0

Browse files
github-actions[bot]svelte-docs-bot[bot]Rich-Harris
authored
Sync kit docs (#1202)
sync kit docs Co-authored-by: svelte-docs-bot[bot] <196124396+svelte-docs-bot[bot]@users.noreply.github.com> Co-authored-by: Rich Harris <[email protected]>
1 parent f6af4c1 commit 2c961b0

File tree

2 files changed

+94
-1
lines changed

2 files changed

+94
-1
lines changed

apps/svelte.dev/content/docs/kit/20-core-concepts/20-load.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,74 @@ To prevent data waterfalls and preserve layout `load` caches:
714714

715715
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.
716716

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+
export function requireLogin() {
742+
const { locals, url } = getRequestEvent();
743+
744+
// assume `locals.user` is populated in `handle`
745+
if (!locals.user) {
746+
const redirectTo = url.pathname + url.search;
747+
const params = new URLSearchParams({ redirectTo });
748+
749+
redirect(307, `/login?${params}`);
750+
}
751+
752+
return locals.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+
export function requireLogin(): User;
768+
}
769+
770+
// @filename: index.ts
771+
// ---cut---
772+
import { requireLogin } from '$lib/server/auth';
773+
774+
export function load() {
775+
const user = 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
779+
return {
780+
message: `hello ${user.name}!`
781+
};
782+
}
783+
```
784+
717785
## Further reading
718786
719787
- [Tutorial: Loading data](/tutorial/kit/page-data)

apps/svelte.dev/content/docs/kit/98-reference/20-$app-server.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,34 @@ title: $app/server
77

88
```js
99
// @noErrors
10-
import { read } from '$app/server';
10+
import { getRequestEvent, read } from '$app/server';
11+
```
12+
13+
## getRequestEvent
14+
15+
<blockquote class="since note">
16+
17+
Available since 2.20.0
18+
19+
</blockquote>
20+
21+
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`).
24+
25+
<div class="ts-block">
26+
27+
```dts
28+
function getRequestEvent(): RequestEvent<
29+
Partial<Record<string, string>>,
30+
string | null
31+
>;
1132
```
1233

34+
</div>
35+
36+
37+
1338
## read
1439

1540
<blockquote class="since note">

0 commit comments

Comments
 (0)