diff options
Diffstat (limited to 'lib/users')
| -rw-r--r-- | lib/users/repository.ts | 16 | ||||
| -rw-r--r-- | lib/users/send-otp.ts | 75 | ||||
| -rw-r--r-- | lib/users/service.ts | 26 | ||||
| -rw-r--r-- | lib/users/verifyOtp.ts | 23 |
4 files changed, 100 insertions, 40 deletions
diff --git a/lib/users/repository.ts b/lib/users/repository.ts index 78d1668b..3a404bde 100644 --- a/lib/users/repository.ts +++ b/lib/users/repository.ts @@ -111,8 +111,6 @@ export const getOtpByEmailAndCode = async ( code: string ): Promise<Otp | null> => { - console.log(email, code, "db") - const [otp] = await db .select() .from(otps) @@ -123,6 +121,20 @@ export const getOtpByEmailAndCode = async ( return otp ?? null; }; +export const getOtpByEmail = async ( + email: string, +): Promise<User | null> => { + + const [user] = await db + .select() + .from(users) + .where( + eq(users.email, email) + ); + + return user ?? null; +}; + export async function findAllRoles(): Promise<Role[]> { return db.select().from(roles).where(eq(roles.domain ,'evcp')).orderBy(asc(roles.name)); }
\ No newline at end of file diff --git a/lib/users/send-otp.ts b/lib/users/send-otp.ts index 55c08eaf..ecaf19a5 100644 --- a/lib/users/send-otp.ts +++ b/lib/users/send-otp.ts @@ -27,48 +27,49 @@ export async function sendOtpAction(email: string, lng: string) { }; } + /////테스트 임시 // OTP 및 만료 시간 생성 - const otp = Math.floor(100000 + Math.random() * 900000).toString(); - const expires = new Date(Date.now() + 10 * 60 * 1000); // 10분 후 만료 - const token = jwt.sign( - { - email, - otp, - exp: Math.floor(expires.getTime() / 1000), - }, - process.env.JWT_SECRET! - ); + // const otp = Math.floor(100000 + Math.random() * 900000).toString(); + // const expires = new Date(Date.now() + 10 * 60 * 1000); // 10분 후 만료 + // const token = jwt.sign( + // { + // email, + // otp, + // exp: Math.floor(expires.getTime() / 1000), + // }, + // process.env.JWT_SECRET! + // ); - // DB에 OTP 추가 - await addNewOtp(email, otp, new Date(), token, expires); + // // DB에 OTP 추가 + // await addNewOtp(email, otp, new Date(), token, expires); - // 이메일에서 사용할 URL 구성 - const verificationUrl = `http://${host}/ko/login?token=${token}`; + // // 이메일에서 사용할 URL 구성 + // const verificationUrl = `http://${host}/ko/login?token=${token}`; - // IP 정보로부터 지역 조회 (ip-api 사용) - const ip = headersList.get('x-forwarded-for')?.split(',')[0]?.trim() || ''; - let location = ''; - try { - const response = await fetch(`http://ip-api.com/json/${ip}?fields=country,city`); - const data = await response.json(); - location = data.city && data.country ? `${data.city}, ${data.country}` : ''; - } catch (error) { - // 위치 조회 실패 시 무시 - } + // // IP 정보로부터 지역 조회 (ip-api 사용) + // const ip = headersList.get('x-forwarded-for')?.split(',')[0]?.trim() || ''; + // let location = ''; + // try { + // const response = await fetch(`http://ip-api.com/json/${ip}?fields=country,city`); + // const data = await response.json(); + // location = data.city && data.country ? `${data.city}, ${data.country}` : ''; + // } catch (error) { + // // 위치 조회 실패 시 무시 + // } - // OTP 이메일 발송 - await sendEmail({ - to: email, - subject: `${otp} - SHI eVCP Sign-in Verification`, - template: 'otp', - context: { - name: user.name, - otp, - verificationUrl, - location, - language: lng, - }, - }); + // // OTP 이메일 발송 + // await sendEmail({ + // to: email, + // subject: `${otp} - SHI eVCP Sign-in Verification`, + // template: 'otp', + // context: { + // name: user.name, + // otp, + // verificationUrl, + // location, + // language: lng, + // }, + // }); // 클라이언트로 반환할 수 있는 값 return { diff --git a/lib/users/service.ts b/lib/users/service.ts index ae97beed..8b2c927e 100644 --- a/lib/users/service.ts +++ b/lib/users/service.ts @@ -202,6 +202,32 @@ export async function findEmailandOtp(email: string, code: string) { } } +export async function findEmailTemp(email: string) { + try { + + // 2) 사용자 정보 추가로 조회 + const userRecord: User | null = await getUserByEmail(email) + if (!userRecord) { + return null + } + + // 3) 필요한 형태로 "통합된 객체"를 반환 + return { + email: userRecord.email, + name: userRecord.name, // DB 에서 가져온 실제 이름 + id: userRecord.id, // user id + imageUrl:userRecord.imageUrl, + companyId:userRecord.companyId, + domain:userRecord.domain + // 기타 필요한 필드... + } + + } catch (error) { + // 에러 처리 + throw new Error('Failed to fetch user & otp') + } +} + export async function updateUserProfileImage(formData: FormData) { // 1) FormData에서 데이터 꺼내기 const file = formData.get("file") as File | null diff --git a/lib/users/verifyOtp.ts b/lib/users/verifyOtp.ts index aa759338..84919024 100644 --- a/lib/users/verifyOtp.ts +++ b/lib/users/verifyOtp.ts @@ -1,5 +1,5 @@ // lib/users/verifyOtp.ts -import { findEmailandOtp } from '@/lib/users/service' +import { findEmailTemp, findEmailandOtp } from '@/lib/users/service' // "email과 code가 맞으면 유저 정보, 아니면 null" 형태로 작성 export async function verifyOtp(email: string, code: string) { @@ -27,6 +27,27 @@ export async function verifyOtp(email: string, code: string) { } } +export async function verifyOtpTemp(email: string) { + // DB에서 email과 code가 맞는지, 만료 안됐는지 검증 + const otpRecord = await findEmailTemp(email) + if (!otpRecord) { + return null + } + + + // 여기서 otpRecord에 유저 정보가 있다고 가정 + // 예: otpRecord.userId, otpRecord.userName, otpRecord.email 등 + // 실제 DB 설계에 맞춰 필드명을 조정하세요. + return { + email: otpRecord.email, + name: otpRecord.name, + id: otpRecord.id, + imageUrl: otpRecord.imageUrl, + companyId: otpRecord.companyId, + domain: otpRecord.domain, + } +} + export async function verifyExternalCredentials(username: string, password: string) { // DB에서 email과 code가 맞는지, 만료 안됐는지 검증 |
