-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
21 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,32 @@ | ||
import { createServerClient, type CookieOptions } from "@supabase/ssr"; | ||
import { type EmailOtpType } from "@supabase/supabase-js"; | ||
import { cookies } from "next/headers"; | ||
import { NextRequest, NextResponse } from "next/server"; | ||
import { createClient } from "app/supa"; | ||
import { type NextRequest, NextResponse } from "next/server"; | ||
|
||
export async function GET(request: NextRequest) { | ||
const { searchParams } = new URL(request.url); | ||
const token_hash = searchParams.get("token_hash"); | ||
const type = searchParams.get("type") as EmailOtpType | null; | ||
const next = searchParams.get("next") ?? "/"; | ||
|
||
const redirectTo = request.nextUrl.clone(); | ||
redirectTo.pathname = next; | ||
redirectTo.searchParams.delete("token_hash"); | ||
redirectTo.searchParams.delete("type"); | ||
|
||
if (token_hash && type) { | ||
const cookieStore = cookies(); | ||
const supabase = createServerClient( | ||
process.env.NEXT_PUBLIC_SUPABASE_URL!, | ||
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!, | ||
{ | ||
cookies: { | ||
get(name: string) { | ||
return cookieStore.get(name)?.value; | ||
}, | ||
set(name: string, value: string, options: CookieOptions) { | ||
cookieStore.set({ name, value, ...options }); | ||
}, | ||
remove(name: string, options: CookieOptions) { | ||
cookieStore.delete({ name, ...options }); | ||
}, | ||
}, | ||
} | ||
); | ||
const supabase = createClient(); | ||
|
||
const { error } = await supabase.auth.verifyOtp({ | ||
type, | ||
token_hash, | ||
}); | ||
if (!error) { | ||
return NextResponse.redirect(redirectTo); | ||
redirectTo.searchParams.delete("next"); | ||
return NextResponse.redirect(next); | ||
} | ||
} | ||
|
||
// return the user to an error page with some instructions | ||
redirectTo.pathname = "/auth/auth-code-error"; | ||
redirectTo.pathname = "/error"; | ||
return NextResponse.redirect(redirectTo); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters