Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(docs): use auth over getSession #9874

Merged
merged 1 commit into from
Feb 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions docs/docs/getting-started/providers/oauth-tutorial.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -245,14 +245,14 @@ or https://generate-secret.vercel.app/32 to generate a random value for it.

### Exposing the session via page store

Auth.js provides us a getSession, function to access the session data and status, to call from the `event.locals` variable. We can now just call it and add it to our `$page` store.
Auth.js provides us a `auth`, function to access the session data and status, to call from the `event.locals` variable. We can now just call it and add it to our `$page` store.

```ts
import type { LayoutServerLoad } from "./$types"

export const load: LayoutServerLoad = async (event) => {
return {
session: await event.locals.getSession(),
session: await event.locals.auth(),
}
}
```
Expand All @@ -279,14 +279,14 @@ You can use the `$page.data.session` variable from anywhere on your page. Learn

### Protecting API Routes

To protect your API Routes (blocking unauthorized access to resources), you can use `locals.getSessions()` just like in the layouts file to know whether a session exists or not:
To protect your API Routes (blocking unauthorized access to resources), you can use `locals.auth()` just like in the layouts file to know whether a session exists or not:

```ts title="routes/api/movies/+server.ts"
import { json, error } from "@sveltejs/kit"
import type { RequestEvent } from "./$types"

export async function GET({ locals }: RequestEvent) {
const session = await locals.getSession()
const session = await locals.auth()
if (!session?.user) {
throw error(401, "You must sign in to view movies.")
}
Expand Down
Loading