const bcrypt = require('bcryptjs'); const User = require('../models/user.model'); const jwt = require('jsonwebtoken'); const authConfig = require('../configs/auth.config'); const constant = require('../util/constant') const nodemailer = require('nodemailer'); const signUp = async(req,res) => { try{ const obj = { name : req.body.name, password : bcrypt.hashSync(req.body.password,8), email : req.body.email, registerWith : constant.registerWith.Email } const generateOTP = () => { return Math.floor(10000 + Math.random() * 90000); } const email = req.body.email; const otp = generateOTP(); obj['otp'] = otp; const user = await User.create(obj); const transporter = nodemailer.createTransport({ service: 'Gmail', // e.g., 'Gmail' auth: { user: 'checkdemo02@gmail.com', pass: 'vqdoqmekygtousli' } }); const mailOptions = { from: 'checkdemo02@gmail.com', to: req.body.email, // The user's email subject: 'Your OTP Code', text: `Your OTP code is: ${otp}` }; // Send the email transporter.sendMail(mailOptions, (error, info) => { if (error) { console.error('Error sending OTP email:', error); return res.status(500).send({ errorCode : 500, message : "Mail not Send" }) } else { console.log('OTP email sent:', info.response); } }); const token = jwt.sign({id:user._id},authConfig.secretKey,{ expiresIn : 6000000 }); return res.status(201).send({ error_code : 200, message : 'Success', otp : otp, accessToken : token }) }catch(err){ console.log(err); res.status(500).send({ error_code : 200, message : 'Failed' }) } } const signIn = async (req, res) => { try { const { email, password } = req.body; if (!email){ return res.status(400).json({ error_code: 400, message: 'Email is required' }); } if (!password){ return res.status(400).json({ error_code: 400, message: 'Password is required' }); } // Find user by email const user = await User.findOne({ email }); console.log("🚀 ~ signIn ~ user:", user) if (!user) { return res.status(404).json({ error_code: 404, message: 'User not found' }); } // Check if the provided password matches the user's password const isPasswordValid = bcrypt.compareSync(password, user.password); if (!isPasswordValid) { return res.status(401).json({ error_code: 401, message: 'Password is incorrect' }); } // Generate JWT token const token = jwt.sign({ id: user._id,userTypes:user.userTypes }, authConfig.secretKey, { expiresIn: '1h' }); console.log("🚀 ~ signIn ~ token:", token) return res.status(200).json({ error_code: 200, message: 'User logged in successfully', accessToken: token }); } catch (err) { console.error('Error inside signIn:', err); return res.status(500).json({ error_code: 500, message: 'Internal Server Error' }); } }; const verifyOtp = async(req,res,next) => { try{ const otp = req.body.otp; const user = await User.findOne({_id:req.userId}); console.log(user.otp,otp); if(user.otp!=otp){ return res.status(400).send({ error_code : 400, message : 'otp is incorrect' }) } return res.status(200).send({ error_code : 200, message : 'otp Verified' }) }catch(err){ console.log('Error inside verifyOtp Controller',err); return res.status(500).send({ message : 'Internal Error', error_code : 500, }) } } module.exports = { signIn, signUp, verifyOtp }