Skip to content

Commit

Permalink
docs: update cookie examples with path (#11422)
Browse files Browse the repository at this point in the history
  • Loading branch information
benmccann authored Dec 21, 2023
1 parent 2a091dc commit 65b81de
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions documentation/docs/20-core-concepts/30-form-actions.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ Each action receives a `RequestEvent` object, allowing you to read the data with
```js
// @errors: 2304
/// file: src/routes/login/+page.server.js
import { base } from '$app/paths';

/** @type {import('./$types').PageServerLoad} */
export async function load({ cookies }) {
const user = await db.getUserFromSession(cookies.get('sessionid'));
Expand All @@ -122,7 +124,7 @@ export const actions = {
const password = data.get('password');

const user = await db.getUser(email);
cookies.set('sessionid', await db.createSession(user));
cookies.set('sessionid', await db.createSession(user), { path: base});

return { success: true };
},
Expand Down Expand Up @@ -156,6 +158,7 @@ If the request couldn't be processed because of invalid data, you can return val
```diff
/// file: src/routes/login/+page.server.js
+import { fail } from '@sveltejs/kit';
import { base } from '$app/paths';

/** @type {import('./$types').Actions} */
export const actions = {
Expand All @@ -174,7 +177,7 @@ export const actions = {
+ return fail(400, { email, incorrect: true });
+ }

cookies.set('sessionid', await db.createSession(user));
cookies.set('sessionid', await db.createSession(user), { path: base });

return { success: true };
},
Expand Down Expand Up @@ -214,6 +217,7 @@ Redirects (and errors) work exactly the same as in [`load`](load#redirects):
```diff
/// file: src/routes/login/+page.server.js
+import { fail, redirect } from '@sveltejs/kit';
import { base } from '$app/paths';

/** @type {import('./$types').Actions} */
export const actions = {
Expand All @@ -231,7 +235,7 @@ export const actions = {
return fail(400, { email, incorrect: true });
}

cookies.set('sessionid', await db.createSession(user));
cookies.set('sessionid', await db.createSession(user), { path: base });

+ if (url.searchParams.has('redirectTo')) {
+ redirect(303, url.searchParams.get('redirectTo'));
Expand Down Expand Up @@ -293,6 +297,8 @@ declare namespace App {

// @filename: index.js
// ---cut---
import { base } from '$app/paths';

/** @type {import('./$types').PageServerLoad} */
export function load(event) {
return {
Expand All @@ -303,7 +309,7 @@ export function load(event) {
/** @type {import('./$types').Actions} */
export const actions = {
logout: async (event) => {
event.cookies.delete('sessionid');
event.cookies.delete('sessionid', { path: base });
event.locals.user = null;
}
};
Expand Down

0 comments on commit 65b81de

Please sign in to comment.