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

docs: update cookie examples with path #11422

Merged
merged 1 commit into from
Dec 21, 2023
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
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
Loading