From e0dfb55c5457aec489fc084c4567e791b4c65eb1 Mon Sep 17 00:00:00 2001 From: dujinkim Date: Wed, 26 Mar 2025 00:37:41 +0000 Subject: 3/25 까지의 대표님 작업사항 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/users/verifyToken.ts | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 lib/users/verifyToken.ts (limited to 'lib/users/verifyToken.ts') diff --git a/lib/users/verifyToken.ts b/lib/users/verifyToken.ts new file mode 100644 index 00000000..745a1052 --- /dev/null +++ b/lib/users/verifyToken.ts @@ -0,0 +1,38 @@ +"use server"; + +import jwt from 'jsonwebtoken'; +import { findOtpByEmailandToken } from '@/lib/users/service'; + +export async function verifyTokenAction(token: string) { + if (!token) { + // 토큰이 없으면 바로 false 반환 + return { valid: false }; + } + + try { + // 토큰 검증 + const decoded = jwt.verify(token, process.env.JWT_SECRET!) as { email: string; otp: string }; + const { email } = decoded; + + // DB에서 OTP 정보 조회 + const otp = await findOtpByEmailandToken(email, token); + if (!otp) { + // 해당하는 OTP/토큰이 없으면 invalid + return { valid: false }; + } + + // 토큰 동일성 및 만료 확인 + if (otp.otpToken !== token || (otp.otpExpires && otp.otpExpires < new Date())) { + return { valid: false }; + } + + // 여기까지 통과하면 valid + return { + valid: true, + email, + }; + } catch (error) { + // JWT 검증 실패 + return { valid: false }; + } +} \ No newline at end of file -- cgit v1.2.3