|
| 1 | +import { Request, Response, NextFunction } from 'express'; |
| 2 | +import { logger } from '../../utils/logger'; |
| 3 | +import { emailSchema, registerSchema } from './auth.schema'; |
| 4 | +import { sendOtpEmail } from '../../Services/email.service'; |
| 5 | +import { generateotp } from '../../utils/otp'; |
| 6 | +import { appDataSouce } from '../../data-source'; |
| 7 | +import { Otp } from '../../entities/opt'; |
| 8 | +import { User } from '../../entities/User'; |
| 9 | + |
| 10 | +export const sendOtp = async ( |
| 11 | + req: Request, |
| 12 | + res: Response, |
| 13 | + next: NextFunction, |
| 14 | +) => { |
| 15 | + try { |
| 16 | + logger.info('reached'); |
| 17 | + const result = emailSchema.safeParse(req.body); |
| 18 | + if (!result.success) { |
| 19 | + return res |
| 20 | + .status(400) |
| 21 | + .json({ message: 'validation vailed', error: result.error?.format() }); |
| 22 | + } |
| 23 | + const otpRep = appDataSouce.getRepository(Otp); |
| 24 | + const otpcode = generateotp(); |
| 25 | + logger.debug({ otpcode }, 'otp is'); |
| 26 | + const email = result.data?.email; |
| 27 | + await sendOtpEmail(email.toString(), otpcode.toString()); |
| 28 | + await otpRep.delete({ email }); |
| 29 | + await otpRep.save({ |
| 30 | + email, |
| 31 | + otp: otpcode.toString(), |
| 32 | + expiresAt: new Date(Date.now() + 5 * 60 * 1000), |
| 33 | + }); |
| 34 | + res.status(200).json({ message: 'otp sent success fully ', success: true }); |
| 35 | + } catch (err) { |
| 36 | + logger.error('error in register'); |
| 37 | + next(err); |
| 38 | + } |
| 39 | +}; |
| 40 | + |
| 41 | +export const verifyotp = async ( |
| 42 | + req: Request, |
| 43 | + res: Response, |
| 44 | + next: NextFunction, |
| 45 | +) => { |
| 46 | + try { |
| 47 | + const { email, otp } = req.body; |
| 48 | + if (!otp || !email) { |
| 49 | + return res.status(400).json({ message: ' otp and email are required' }); |
| 50 | + } |
| 51 | + |
| 52 | + const otpRepo = appDataSouce.getRepository(Otp); |
| 53 | + const otpRecord = await otpRepo.findOne({ |
| 54 | + where: { |
| 55 | + email, |
| 56 | + verified: false, |
| 57 | + }, |
| 58 | + order: { |
| 59 | + createdAt: 'DESC', |
| 60 | + }, |
| 61 | + }); |
| 62 | + if (!otpRecord) { |
| 63 | + return res.status(400).json({ |
| 64 | + messge: 'Ivalid or expired OTP', |
| 65 | + }); |
| 66 | + } |
| 67 | + |
| 68 | + if (otpRecord.expiresAt < new Date()) { |
| 69 | + return res.status(400).json({ message: 'OTP has expired' }); |
| 70 | + } |
| 71 | + if (otpRecord.otp != otp.toString()) { |
| 72 | + return res.status(400).json({ message: 'Invalid OTP' }); |
| 73 | + } |
| 74 | + otpRecord.verified = true; |
| 75 | + await otpRepo.save(otpRecord); |
| 76 | + res.status(200).json({ |
| 77 | + message: 'OTP verified successfully', |
| 78 | + success: true, |
| 79 | + otpId: otpRecord.id, |
| 80 | + }); |
| 81 | + } catch (err) { |
| 82 | + logger.error({ err }, 'error in verify-otp'); |
| 83 | + next(err); |
| 84 | + } |
| 85 | +}; |
| 86 | + |
| 87 | +export const register = async ( |
| 88 | + req: Request, |
| 89 | + res: Response, |
| 90 | + next: NextFunction, |
| 91 | +) => { |
| 92 | + try { |
| 93 | + const result = registerSchema.safeParse(req.body); |
| 94 | + if (!result.success) { |
| 95 | + return res.status(400).json({ |
| 96 | + message: 'invalid registation data', |
| 97 | + errors: result.error.format(), |
| 98 | + }); |
| 99 | + } |
| 100 | + const { otpId, name, age, gender, interests } = result.data; |
| 101 | + if (!otpId) { |
| 102 | + return res.status(400).json({ message: 'otpid requuired' }); |
| 103 | + } |
| 104 | + const otpRepo = appDataSouce.getRepository(Otp); |
| 105 | + const userRepo = appDataSouce.getRepository(User); |
| 106 | + |
| 107 | + const otpRecord = await otpRepo.findOne({ |
| 108 | + where: { id: otpId }, |
| 109 | + }); |
| 110 | + if (!otpRecord) { |
| 111 | + return res.status(400).json({ message: 'invalid registration ' }); |
| 112 | + } |
| 113 | + if (!otpRecord.verified) { |
| 114 | + return res.status(400).json({ |
| 115 | + message: 'OTP not verified', |
| 116 | + }); |
| 117 | + } |
| 118 | + |
| 119 | + const email = otpRecord.email; |
| 120 | + |
| 121 | + const existingUser = await userRepo.findOne({ |
| 122 | + where: { email }, |
| 123 | + }); |
| 124 | + |
| 125 | + if (existingUser) { |
| 126 | + return res.status(409).json({ message: 'User already exists' }); |
| 127 | + } |
| 128 | + |
| 129 | + const user = userRepo.create({ email, name, age, gender, interests }); |
| 130 | + |
| 131 | + await userRepo.save(user); |
| 132 | + await otpRepo.delete({ id: otpId }); |
| 133 | + return res |
| 134 | + .status(201) |
| 135 | + .json({ message: 'User registered successfully', success: true }); |
| 136 | + } catch (err) { |
| 137 | + logger.error({ err }, 'error in register'); |
| 138 | + next(err); |
| 139 | + } |
| 140 | +}; |
0 commit comments