Skip to content

Commit

Permalink
--amend
Browse files Browse the repository at this point in the history
  • Loading branch information
ambroisegithub committed May 16, 2024
1 parent 6f60742 commit 1944cb9
Showing 1 changed file with 39 additions and 24 deletions.
63 changes: 39 additions & 24 deletions src/controller/userController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import UserModel from '../database/models/userModel';
import sendEmail from '../emails/index';
import { sendCode } from '../emails/mailer';
import jwt from 'jsonwebtoken';
import errorHandler from '../middlewares/errorHandler'
import errorHandler from '../middlewares/errorHandler';

// Assuming dbConnection.getRepository(UserModel) returns a repository instance
const userRepository = dbConnection.getRepository(UserModel);
Expand All @@ -18,7 +18,7 @@ interface CreateUserRequestBody {
lastName: string;
email: string;
password: string;
userType: 'vendor' | 'buyer';
userType: 'Admin' | 'vendor' | 'buyer';
}

// Define validation and sanitization rules
Expand Down Expand Up @@ -48,9 +48,12 @@ export const registerUser = [
const hashedPassword = await bcrypt.hash(password, 10);

const userRole =
userType == 'vendor'
? (await roleRepository.findOneBy({ name: 'Vendor' }))!
: (await roleRepository.findOneBy({ name: 'Buyer' }))!;
userType === 'vendor'
? (await roleRepository.findOneBy({ name: 'Vendor' }))!
: userType === 'buyer'
? (await roleRepository.findOneBy({ name: 'Buyer' }))!
: (await roleRepository.findOneBy({ name: 'Admin' }))!;


const newUser = new UserModel({
firstName: firstName,
Expand Down Expand Up @@ -164,14 +167,13 @@ export const deleteUser = async (req: Request, res: Response) => {
return res
.status(500)
.json({ error: 'An error occurred while deleting the record.' });
}
};

}}


export const Login = errorHandler(async (req: Request, res: Response) => {
const user = await userRepository.findOne({
where: { email: req.body['email'] },
relations: ['userType']
export const Login = errorHandler(async (req: Request, res: Response) => {
const user = await userRepository.findOne({
where: { email: req.body['email'] },
relations: ['userType'],
});
if (!user) {
return res.status(404).send({ message: 'User Not Found' });
Expand All @@ -187,26 +189,35 @@ export const deleteUser = async (req: Request, res: Response) => {
{ expiresIn: '1d' }
);
const confirmLink = `${process.env.APP_URL}/api/v1/confirm?token=${token}`;
await sendEmail('confirm', user.email, { name: user.firstName, link: confirmLink });
return res.status(401).send({ message: 'Please verify your email. Confirmation link has been sent.' });
await sendEmail('confirm', user.email, {
name: user.firstName,
link: confirmLink,
});
return res.status(401).send({
message: 'Please verify your email. Confirmation link has been sent.',
});
}

if (user.userType.name === 'Vendor') {
const twoFactorCode = Math.floor(100000 + Math.random() * 900000);

await userRepository.update(user.id, { twoFactorCode });

await sendCode(
user.email,
'Your 2FA Code',
'./templates/2fa.html',
{ name: user.firstName, twoFactorCode: twoFactorCode.toString() }
);
await sendCode(user.email, 'Your 2FA Code', './templates/2fa.html', {
name: user.firstName,
twoFactorCode: twoFactorCode.toString(),
});

res.status(200).json({ message: 'Please provide the 2FA code sent to your email.' });
res
.status(200)
.json({ message: 'Please provide the 2FA code sent to your email.' });
} else {
const token = jwt.sign({ userId: user.id }, process.env.JWT_SECRET as jwt.Secret, { expiresIn: '1h' });
res.status(200).json({ token, message: 'Buyer Logged in successfully'});
const token = jwt.sign(
{ userId: user.id },
process.env.JWT_SECRET as jwt.Secret,
{ expiresIn: '1h' }
);
res.status(200).json({ token, message: 'Buyer Logged in successfully' });
}
});

Expand All @@ -223,6 +234,10 @@ export const verify2FA = errorHandler(async (req: Request, res: Response) => {
return res.status(401).json({ error: 'Invalid code' });
}

const token = jwt.sign({ userId: user.id }, process.env.JWT_SECRET as jwt.Secret, { expiresIn: '1h' });
const token = jwt.sign(
{ userId: user.id },
process.env.JWT_SECRET as jwt.Secret,
{ expiresIn: '1h' }
);
return res.status(200).json({ token });
});

0 comments on commit 1944cb9

Please sign in to comment.