Skip to content

Commit a12c670

Browse files
committed
add login redirect for protected
1 parent a787b02 commit a12c670

4 files changed

Lines changed: 19 additions & 8 deletions

File tree

apps/codingcatdev/src/routes/(protected)/account/+layout.server.ts renamed to apps/codingcatdev/src/routes/(protected)/+layout.server.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@ import { ccdValidateSessionCookie } from '$lib/server/firebase';
22
import { redirect } from '@sveltejs/kit';
33
import type { LayoutServerLoad } from './$types';
44

5-
export const load = (async ({ cookies }) => {
5+
export const load = (async ({ cookies, route, params, url }) => {
66
const ccdsession = cookies.get('session');
77

88
if (!ccdsession) {
9-
throw redirect(307, '/login');
9+
throw redirect(307, `/login?redirectTo=${url.pathname}`);
1010
}
1111

1212
const decodedClaims = await ccdValidateSessionCookie(ccdsession);
1313

1414
if (!decodedClaims) {
15-
throw redirect(307, '/login');
15+
throw redirect(307, `/login?redirectTo=${url.pathname}`);
1616
}
1717

1818
return {

‎apps/codingcatdev/src/routes/login/+page.server.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { fail, redirect } from '@sveltejs/kit'
44

55
export const actions = {
66
login: async ({ cookies, url }) => {
7+
console.log(url)
78
const ccdLoginIdToken = cookies.get('__ccdlogin');
89

910
if (!ccdLoginIdToken) {
@@ -29,7 +30,11 @@ export const actions = {
2930
}
3031
} satisfies Actions;
3132

32-
export const load = (async ({ parent }) => {
33+
export const load = (async ({ parent, url }) => {
3334
const { user } = await parent();
3435
if (user) throw redirect(303, '/account');
36+
37+
return {
38+
redirectTo: url.searchParams.get('redirectTo') || ''
39+
}
3540
}) satisfies PageServerLoad;

‎apps/codingcatdev/src/routes/login/+page.svelte‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,20 @@
1212
import TwitterAuth from './TwitterAuth.svelte';
1313
import GitHubAuth from './GitHubAuth.svelte';
1414
15-
export let form: ActionData;
15+
// TODO: More actions
16+
// export let form: ActionData;
17+
18+
import type { PageData } from './$types';
19+
export let data: PageData;
20+
$: action = `?/login${data.redirectTo && '&redirectTo=' + data.redirectTo}`;
1621
</script>
1722

1823
<LayoutWrapper>
1924
<section class="bcu-card flex flex-col gap-4 p-4">
2025
<form
2126
class="flex justify-center w-full"
2227
method="POST"
23-
action="?/login"
28+
{action}
2429
use:enhance={async ({ action, cancel, controller, data, form }) => {
2530
// `form` is the `<form>` element
2631
// `data` is its `FormData` object
@@ -71,7 +76,7 @@
7176

7277
<hr />
7378

74-
<GoogleAuth />
79+
<GoogleAuth {action} />
7580
<TwitterAuth />
7681
<GitHubAuth />
7782
</section>

‎apps/codingcatdev/src/routes/login/GoogleAuth.svelte‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@
99
1010
// Display
1111
import { enhance } from '$app/forms';
12+
export let action: string;
1213
</script>
1314

1415
<form
1516
class="flex justify-center w-full"
1617
method="POST"
17-
action="?/login"
18+
{action}
1819
use:enhance={async ({ action, cancel, controller, data, form }) => {
1920
await ccdSignInWithPopUp(provider);
2021

0 commit comments

Comments
 (0)