diff options
| author | dujinkim <dujin.kim@dtsolution.co.kr> | 2025-03-26 00:37:41 +0000 |
|---|---|---|
| committer | dujinkim <dujin.kim@dtsolution.co.kr> | 2025-03-26 00:37:41 +0000 |
| commit | e0dfb55c5457aec489fc084c4567e791b4c65eb1 (patch) | |
| tree | 68543a65d88f5afb3a0202925804103daa91bc6f /lib/users/verifyToken.ts | |
3/25 까지의 대표님 작업사항
Diffstat (limited to 'lib/users/verifyToken.ts')
| -rw-r--r-- | lib/users/verifyToken.ts | 38 |
1 files changed, 38 insertions, 0 deletions
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 |
