mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-15 02:15:21 +00:00
Refactor and enhance user authentication in AuthService
This commit is contained in:
@ -33,32 +33,33 @@ 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');
|
||||||
return result;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return null;
|
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||||
|
const { password, ...result } = user;
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
async createSession(data): Promise<UserSessionEntity> {
|
async createSession(data): Promise<UserSessionEntity> {
|
||||||
|
Reference in New Issue
Block a user