Skip to content

Commit

Permalink
feat: use one-time code for login (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
dustinwhisman authored Dec 23, 2023
1 parent 73ce359 commit cb2f1ab
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 30 deletions.
6 changes: 0 additions & 6 deletions src/routes/auth/check-email/+page.svelte

This file was deleted.

10 changes: 10 additions & 0 deletions src/routes/auth/confirm/+page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { redirect } from '@sveltejs/kit';

export const load = async ({ url }) => {
const email = url.searchParams.get('email') ?? '';
if (!email) {
throw redirect(303, '/auth/login');
}

return { email };
};
16 changes: 16 additions & 0 deletions src/routes/auth/confirm/+page.server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { fail, redirect } from '@sveltejs/kit';

export const actions = {
default: async ({ request, locals: { supabase } }) => {
const formData = await request.formData();
const email = formData.get('email');
const token = formData.get('token');

const { error } = await supabase.auth.verifyOtp({ email, token, type: 'email' });
if (error) {
return fail(500, { message: 'Server error. Try again later.', success: false, email });
}

throw redirect(303, '/');
},
};
13 changes: 13 additions & 0 deletions src/routes/auth/confirm/+page.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<script>
export let data;
</script>

<h1>Confirm Login</h1>
<p>Check your email for a one-time code that you can use to confirm your login.</p>

<form method="POST">
<input type="hidden" name="email" value={data.email} />
<label for="token">One-time code</label>
<input id="token" type="text" name="token" inputmode="numeric" autocomplete="one-time-code" />
<button type="submit">Finish logging in</button>
</form>
21 changes: 0 additions & 21 deletions src/routes/auth/confirm/+server.js

This file was deleted.

6 changes: 3 additions & 3 deletions src/routes/auth/login/+page.server.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { fail, redirect } from '@sveltejs/kit';

export const actions = {
default: async ({ request, url: { origin }, locals: { supabase } }) => {
default: async ({ request, locals: { supabase } }) => {
const formData = await request.formData();
const email = formData.get('email');

const { error } = await supabase.auth.signInWithOtp({ email, redirectTo: origin });
const { error } = await supabase.auth.signInWithOtp({ email });
if (error) {
return fail(500, { message: 'Server error. Try again later.', success: false, email });
}

throw redirect(303, '/auth/check-email');
throw redirect(303, `/auth/confirm?email=${email}`);
},
};

0 comments on commit cb2f1ab

Please sign in to comment.