Skip to content
This repository has been archived by the owner on Jan 14, 2024. It is now read-only.

Commit

Permalink
fix: authn redirect to homepage instead of previous page
Browse files Browse the repository at this point in the history
  • Loading branch information
sunxyw committed Nov 26, 2023
1 parent 73d65c0 commit be12d2a
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 18 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"@typescript-eslint/parser": "^6.10.0",
"@unpic/svelte": "^0.0.38",
"@zerodevx/svelte-img": "^2.1.0",
"async-wait-until": "^2.0.12",
"autoprefixer": "^10.4.16",
"bcp-47": "^2.1.0",
"browserslist": "^4.22.1",
Expand Down
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion src/lib/shared/layout/header/UserClip.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
}
return profile.data;
});
$: loginLink = '/auth?redirect=' + encodeURIComponent($page.url.pathname);
</script>

{#await userFunc}
Expand All @@ -40,5 +42,5 @@
{user.nickname}
</Button>
{:catch error}
<Button variant="outline" class={clazz} href="/auth">Login</Button>
<Button variant="outline" class={clazz} href={loginLink}>Login</Button>
{/await}
24 changes: 15 additions & 9 deletions src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { browser } from '$app/environment';
import { navigating, page } from '$app/stores';
import { QueryClientProvider } from '@tanstack/svelte-query';
import { waitUntil } from 'async-wait-until';
import { onMount } from 'svelte';
import toast, { Toaster } from 'svelte-french-toast';
Expand Down Expand Up @@ -46,20 +47,25 @@
}
}
const showFlashToast = async (flash: { type: 'success' | 'error'; message: string }) => {
await waitUntil(() => !loading);
if (flash.type === 'success') {
toast.success(flash.message);
} else if (flash.type === 'error') {
toast.error(flash.message);
} else {
toast(flash.message);
}
};
const flash = getFlash(page);
flash.subscribe((v) => {
flash.subscribe(async (v) => {
if (!v) {
return;
}
if (v.type === 'success') {
toast.success(v.message);
} else if (v.type === 'error') {
toast.error(v.message);
} else {
toast(v.message);
}
await showFlashToast(v);
flash.set(undefined);
});
</script>
Expand Down
2 changes: 1 addition & 1 deletion src/routes/auth/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
];
const handleSocialLogin = async (provider: AuthnSocialProvider) => {
await signIn($page.data.supabase.auth, provider);
await signIn($page.data.supabase.auth, provider, $page.url.searchParams.get('redirect'));
};
</script>

Expand Down
12 changes: 8 additions & 4 deletions src/routes/auth/callback/+server.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { redirect } from '@sveltejs/kit';
import { redirect } from 'sveltekit-flash-message/server';

import type { RequestHandler } from './$types';

Expand All @@ -11,13 +11,17 @@ export const GET: RequestHandler = async (event) => {
const next = url.searchParams.get('next') ?? '/';

if (!code) {
throw redirect(303, '/auth/auth-code-missing');
throw redirect('/auth', { type: 'error', message: 'No auth code provided.' }, event);
}

const { error } = await supabase.auth.exchangeCodeForSession(code);
if (error) {
throw redirect(303, '/auth/auth-code-error');
throw redirect('/auth', { type: 'error', message: 'Failed to validate auth code.' }, event);
}

throw redirect(303, `/${next.slice(1)}?auth=success`);
throw redirect(
`/${next.slice(1)}`,
{ type: 'success', message: 'You are now logged in!' },
event
);
};
4 changes: 1 addition & 3 deletions src/routes/games/proposals/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { fail } from '@sveltejs/kit';
import { redirect, setFlash } from 'sveltekit-flash-message/server';
import { message, setError, superValidate } from 'sveltekit-superforms/server';
import { superValidate } from 'sveltekit-superforms/server';

import { formSchema } from './schema';

Expand All @@ -20,15 +20,13 @@ export const actions: Actions = {
}

const { error } = await event.locals.supabase.from('games').insert(form.data);
console.log(error);

if (error) {
setFlash({ type: 'error', message: 'There was an error saving your game proposal.' }, event);
return fail(400, { form });
}

throw redirect(
303,
'/games',
{ type: 'success', message: 'Your game proposal has been recorded.' },
event
Expand Down

0 comments on commit be12d2a

Please sign in to comment.