From 9d3cc8badb3a172eb9712013b57355846f7b8bb4 Mon Sep 17 00:00:00 2001 From: Korbs Date: Wed, 17 Jul 2024 03:19:00 -0400 Subject: [PATCH] Add email verification area with OTP field --- src/pages/api/auth/confirm.ts | 20 ++++++++++++++++++++ src/pages/api/auth/register.ts | 3 ++- src/pages/email-confirm.astro | 20 ++++++++++++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 src/pages/api/auth/confirm.ts create mode 100644 src/pages/email-confirm.astro diff --git a/src/pages/api/auth/confirm.ts b/src/pages/api/auth/confirm.ts new file mode 100644 index 0000000..9e7fc1a --- /dev/null +++ b/src/pages/api/auth/confirm.ts @@ -0,0 +1,20 @@ +import type { APIRoute } from "astro" +import { supabase } from "@library/supabase" + +export const POST: APIRoute = async ({ request, redirect }) => { + const formData = await request.formData() + const EMAIL = formData.get("email")?.toString() + const OTP = formData.get("code")?.toString() + + if (!OTP) { + return new Response("Code required", { status: 400 }) + } + + const { error } = await supabase.auth.verifyOtp({type: 'email', token: OTP, email: EMAIL}) + + if (error) { + return new Response(error.message, { status: 500 }) + } + + return redirect("/?=welcome") +} diff --git a/src/pages/api/auth/register.ts b/src/pages/api/auth/register.ts index 42dfa6d..aae5360 100644 --- a/src/pages/api/auth/register.ts +++ b/src/pages/api/auth/register.ts @@ -15,6 +15,7 @@ export const POST: APIRoute = async ({ request, redirect }) => { // https://supabase.com/docs/reference/javascript/auth-signup?example=sign-up-with-additional-user-metadata const { error } = await supabase.auth.signUp({ options: { + emailRedirectTo: "http://localhost:4321/?=welcome", data: { full_name: name, avatar_url: avatar_url, @@ -32,5 +33,5 @@ export const POST: APIRoute = async ({ request, redirect }) => { return new Response(error.message, { status: 500 }) } - return redirect("/") + return redirect("/email-confirm") } diff --git a/src/pages/email-confirm.astro b/src/pages/email-confirm.astro new file mode 100644 index 0000000..3d2f693 --- /dev/null +++ b/src/pages/email-confirm.astro @@ -0,0 +1,20 @@ +--- +import Base from "@layouts/Base.astro" +--- + + +
+
+

Confimration Code

+
+ + +
+
+ + +
+ +
+
+ \ No newline at end of file