import { Request, Response } from "express"; import userModel from "../../shared/model/user-Model.ts"; import { hashGenerate, hashValidator } from "../../shared/security/Hasing.ts"; export class User { static async signup(req: Request, res: Response) { try { let role; const { userName, email, password, organization, profilePicture } = req.body; const caseChange = email.toLowerCase(); const emailcheck = await userModel(organization).findOne({ email: caseChange, }); if (emailcheck !== null) { res.json({ message: "User already exists", }); } else { const hashpassword = await hashGenerate(password); const userCount = await userModel(organization).countDocuments({}); role = userCount === 0 ? "Admin" : "User"; const isShare = "true"; const newuser = await userModel(organization).create({ userName: userName, email: caseChange, isShare: isShare, password: hashpassword, role: role, profilePicture: profilePicture, }); newuser.save(); res.status(200).json({ message: "New User created", }); } } catch (error: any) { res.status(500).send(error); } } static async login(req: Request, res: Response) { try { let role; const { email, password, organization } = req.body; const existingMail = await userModel(organization).findOne({ email: email, }); if (existingMail === null || !existingMail) { res.status(401).json({ message: "User Not Found!!! Kindly signup..." }); } else { const hashedpassword = existingMail.password; const checkpassword = await hashValidator(password, hashedpassword); if (checkpassword) { res.status(200).send({ message: "login successfull", email: existingMail.email, name: existingMail.userName, userId: existingMail._id, isShare: existingMail.isShare, }); } else { res.status(404).json({ message: "email & password is invalid...Check the credentials", }); } } } catch (error: any) { res.status(500).send(error); } } }