fix(tutorial): update redirect in SvelteKit SSR auth tutorial

The current redirect status code is 301, but that is a permanent
redirect. The problem with this is the browser will cache the redirect
and redirect for every request without even making a request to the
server.

Instead, we should use a 302 status code, which is a temporary redirect.
So, when the user hits the home page, the browser will hit the server
so that the server checks whether the user is logged in or not and
redirect accordingly instead of always redirecting to the same page.
This commit is contained in:
Steven Nguyen
2024-08-16 21:38:30 +00:00
parent ae6077025e
commit 0714b9cbff
3 changed files with 4 additions and 4 deletions

View File

@@ -51,7 +51,7 @@ export const actions = {
});
// Redirect to the account page.
redirect(301, "/account");
redirect(302, "/account");
},
};
```

View File

@@ -15,7 +15,7 @@ import { redirect } from "@sveltejs/kit";
export async function load({ locals }) {
// Logged out users can't access this page.
if (!locals.user) redirect(301, "/signup");
if (!locals.user) redirect(302, "/signup");
// Pass the stored user local to the page.
return {
@@ -34,7 +34,7 @@ export const actions = {
event.cookies.delete(SESSION_COOKIE, { path: "/" });
// Redirect to the sign up page.
redirect(301, "/signup");
redirect(302, "/signup");
},
};
```

View File

@@ -39,7 +39,7 @@ export const actions = {
`${event.url.origin}/signup`
);
redirect(301, redirectUrl);
redirect(302, redirectUrl);
},
};