From e6b33776132eae7386548c3d83462c638c1242fa Mon Sep 17 00:00:00 2001 From: shio <85730998+dino3616@users.noreply.github.com> Date: Mon, 22 Jan 2024 20:45:31 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=E2=9C=A8=20(website/auth)=20challenge?= =?UTF-8?q?=20route=20=E3=82=92=E4=BD=9C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/website/src/app/auth/challenge/route.ts | 54 ++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 apps/website/src/app/auth/challenge/route.ts diff --git a/apps/website/src/app/auth/challenge/route.ts b/apps/website/src/app/auth/challenge/route.ts new file mode 100644 index 0000000..bc284ed --- /dev/null +++ b/apps/website/src/app/auth/challenge/route.ts @@ -0,0 +1,54 @@ +import { getBaseUrl } from '#core/util/get-base-url'; +import { createRouteHandlerClient } from '@supabase/auth-helpers-nextjs'; +import { cookies } from 'next/headers'; +import { type NextRequest, NextResponse } from 'next/server'; +import { relateFingerprintWithUserUseCase } from '#website/use-case/relate-fingerprint-with-user'; +import { clearLockerChallengeUseCase } from '~website/src/use-case/clear-locker-challenge'; + +export const GET = async (request: NextRequest) => { + const cookieStore = cookies(); + + const requestUrl = new URL(request.url); + + const lockerId = requestUrl.searchParams.get('lockerId'); + const hashedFingerprintId = requestUrl.searchParams.get('hashedFingerprintId'); + if (!lockerId || !hashedFingerprintId) { + return NextResponse.redirect(requestUrl); + } + + const supabase = createRouteHandlerClient({ cookies: () => cookieStore }); + const { + data: { user }, + error, + } = await supabase.auth.getUser(); + if (error) { + return NextResponse.redirect(requestUrl, { + status: error.status, + statusText: error.message, + }); + } + + if (!user) { + return NextResponse.redirect(`${requestUrl.origin}?asAuth=true`, { + status: 401, + statusText: 'Unauthorized', + }); + } + + const relatedUser = await relateFingerprintWithUserUseCase(user.id, hashedFingerprintId).catch(() => null); + if (!relatedUser) { + return NextResponse.redirect(requestUrl, { + status: 500, + statusText: 'Internal Server Error', + }); + } + + if ((await clearLockerChallengeUseCase(lockerId).catch(() => null)) === null) { + return NextResponse.redirect(requestUrl, { + status: 500, + statusText: 'Internal Server Error', + }); + } + + return NextResponse.redirect(`${getBaseUrl({ app: 'website' })}`); +};