11import { Request , Response , NextFunction } from 'express' ;
22import { logger } from '../../utils/logger' ;
3- import { emailSchema } from './auth.schema' ;
3+ import { emailSchema , registerSchema } from './auth.schema' ;
44import { sendOtpEmail } from '../../Services/email.service' ;
55import { generateotp } from '../../utils/otp' ;
66import { appDataSouce } from '../../data-source' ;
77import { Otp } from '../../entities/opt' ;
8+ import { User } from '../../entities/User' ;
9+
810export const sendOtp = async (
911 req : Request ,
1012 res : Response ,
@@ -16,22 +18,123 @@ export const sendOtp = async (
1618 if ( ! result . success ) {
1719 return res
1820 . status ( 400 )
19- . json ( { message : 'validation vailed' , error : result . error ?. format ( ) } ) ; }
20- const otpRep = appDataSouce . getRepository ( Otp )
21- const otpcode = generateotp ( )
22- logger . debug ( { otpcode} , "otp is" )
23- const email = result . data ?. email ;
24- await sendOtpEmail ( email . toString ( ) , otpcode . toString ( ) )
25- await otpRep . delete ( { email} )
26- await otpRep . save ( {
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 : {
2755 email,
28- otp :otpcode . toString ( ) ,
29- expiresAt : new Date ( Date . now ( ) + 5 * 60 * 1000 )
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+ }
3067
31- } )
32- res . status ( 200 ) . json ( { message : "otp sent success fully " , success :true } ) ;
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+ } ) ;
3381 } catch ( err ) {
34- logger . error ( 'error in register' ) ;
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' ) ;
35138 next ( err ) ;
36139 }
37140} ;
0 commit comments