Skip to content

Commit

Permalink
fix sveltekit tutorial
Browse files Browse the repository at this point in the history
  • Loading branch information
pilcrowonpaper committed Dec 25, 2023
1 parent 1bc4607 commit 33d2469
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 45 deletions.
45 changes: 1 addition & 44 deletions docs/pages/tutorials/github-oauth/sveltekit.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import { dev } from "$app/environment";
export const lucia = new Lucia(adapter, {
sessionCookie: {
attributes: {
secure: dev
secure: !dev
}
},
getUserAttributes: (attributes) => {
Expand Down Expand Up @@ -203,49 +203,6 @@ interface GitHubUser {

You can validate requests by checking `locals.user`. The field `user.username` is available since we defined the `getUserAttributes()` option. You can protect pages, such as `/`, by redirecting unauthenticated users to the login page.

```ts
const user = Astro.locals.user;
if (!user) {
return Astro.redirect("/login");
}

const username = user.username;
```

## Sign out

Sign out users by invalidating their session with `Lucia.invalidateSession()`. Make sure to remove their session cookie by setting a blank session cookie created with `Lucia.createBlankSessionCookie()`.

```ts
import { lucia } from "../../auth";
import type { APIContext } from "astro";

export async function POST(context: APIContext): Promise<Response> {
if (!context.locals.session) {
return new Response(null, {
status: 401
});
}

await lucia.invalidateSession(context.locals.session.id);

const sessionCookie = lucia.createBlankSessionCookie();
context.cookies.set(sessionCookie.name, sessionCookie.value, sessionCookie.attributes);

return Astro.redirect("/login");
}
```

```html
<form method="post" action="/api/logout">
<button>Sign out</button>
</form>
```

## Validate requests

You can validate requests by checking `locals.user`. The field `user.username` is available since we defined the `getUserAttributes()` option. You can protect pages, such as `/`, by redirecting unauthenticated users to the login page.

```ts
// +page.server.ts
import type { PageServerLoad, Actions } from "./$types";
Expand Down
2 changes: 1 addition & 1 deletion docs/pages/tutorials/username-and-password/sveltekit.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { dev } from "$app/environment";
export const lucia = new Lucia(adapter, {
sessionCookie: {
attributes: {
secure: dev
secure: !dev
}
},
getUserAttributes: (attributes) => {
Expand Down

0 comments on commit 33d2469

Please sign in to comment.