Refactor and enhance user authentication in AuthService

This commit is contained in:
faris Aljohari
2025-01-08 00:14:21 -06:00
parent 38b6cd1a62
commit a8ac2ecae3

View File

@ -33,33 +33,34 @@ export class AuthService {
const user = await this.userRepository.findOne({ const user = await this.userRepository.findOne({
where: { where: {
email, email,
region: regionUuid region: regionUuid ? { uuid: regionUuid } : undefined,
? {
uuid: regionUuid,
}
: undefined,
}, },
relations: ['roleType'], relations: ['roleType'],
}); });
if (!user) {
throw new BadRequestException('Invalid credentials');
}
if (!user.isUserVerified) { if (!user.isUserVerified) {
throw new BadRequestException('User is not verified'); throw new BadRequestException('User is not verified');
} }
if (!user.isActive) { if (!user.isActive) {
throw new BadRequestException('User is not active'); throw new BadRequestException('User is not active');
} }
if (user) {
const passwordMatch = this.helperHashService.bcryptCompare( const passwordMatch = await this.helperHashService.bcryptCompare(
pass, pass,
user.password, user.password,
); );
if (passwordMatch) { if (!passwordMatch) {
const { ...result } = user; throw new BadRequestException('Invalid credentials');
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { password, ...result } = user;
return result; return result;
} }
}
return null;
}
async createSession(data): Promise<UserSessionEntity> { async createSession(data): Promise<UserSessionEntity> {
return await this.sessionRepository.save(data); return await this.sessionRepository.save(data);