mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-26 08:54:54 +00:00
Add UserRoleRepository and RoleTypeRepository imports
This commit is contained in:
@ -22,7 +22,9 @@ export class AuthService {
|
||||
where: {
|
||||
email,
|
||||
},
|
||||
relations: ['role.roleType'],
|
||||
});
|
||||
|
||||
if (!user.isUserVerified) {
|
||||
throw new BadRequestException('User is not verified');
|
||||
}
|
||||
@ -68,7 +70,9 @@ export class AuthService {
|
||||
uuid: user.uuid,
|
||||
type: user.type,
|
||||
sessionId: user.sessionId,
|
||||
roles: user.roles,
|
||||
};
|
||||
|
||||
const tokens = await this.getTokens(payload);
|
||||
await this.updateRefreshToken(user.uuid, tokens.refreshToken);
|
||||
return tokens;
|
||||
|
||||
@ -9,6 +9,8 @@ import { UserAuthService } from './services';
|
||||
import { UserRepository } from '../../libs/common/src/modules/user/repositories';
|
||||
import { UserSessionRepository } from '../../libs/common/src/modules/session/repositories/session.repository';
|
||||
import { UserOtpRepository } from '../../libs/common/src/modules/user-otp/repositories/user-otp.repository';
|
||||
import { UserRoleRepository } from '@app/common/modules/user-role/repositories';
|
||||
import { RoleTypeRepository } from '@app/common/modules/role-type/repositories';
|
||||
|
||||
@Module({
|
||||
imports: [ConfigModule, UserRepositoryModule, CommonModule],
|
||||
@ -19,6 +21,8 @@ import { UserOtpRepository } from '../../libs/common/src/modules/user-otp/reposi
|
||||
UserRepository,
|
||||
UserSessionRepository,
|
||||
UserOtpRepository,
|
||||
UserRoleRepository,
|
||||
RoleTypeRepository,
|
||||
],
|
||||
exports: [AuthenticationService, UserAuthService],
|
||||
})
|
||||
|
||||
@ -1,3 +1,5 @@
|
||||
import { RoleTypeRepository } from './../../../libs/common/src/modules/role-type/repositories/role.type.repository';
|
||||
import { UserRoleRepository } from './../../../libs/common/src/modules/user-role/repositories/user.role.repository';
|
||||
import { UserRepository } from '../../../libs/common/src/modules/user/repositories';
|
||||
import {
|
||||
BadRequestException,
|
||||
@ -16,6 +18,7 @@ import { EmailService } from '../../../libs/common/src/util/email.service';
|
||||
import { OtpType } from '../../../libs/common/src/constants/otp-type.enum';
|
||||
import { UserEntity } from '../../../libs/common/src/modules/user/entities/user.entity';
|
||||
import * as argon2 from 'argon2';
|
||||
import { RoleType } from '@app/common/constants/role.type.enum';
|
||||
|
||||
@Injectable()
|
||||
export class UserAuthService {
|
||||
@ -26,6 +29,8 @@ export class UserAuthService {
|
||||
private readonly helperHashService: HelperHashService,
|
||||
private readonly authService: AuthService,
|
||||
private readonly emailService: EmailService,
|
||||
private readonly userRoleRepository: UserRoleRepository,
|
||||
private readonly roleTypeRepository: RoleTypeRepository,
|
||||
) {}
|
||||
|
||||
async signUp(userSignUpDto: UserSignUpDto): Promise<UserEntity> {
|
||||
@ -33,12 +38,31 @@ export class UserAuthService {
|
||||
if (findUser) {
|
||||
throw new BadRequestException('User already registered with given email');
|
||||
}
|
||||
const salt = this.helperHashService.randomSalt(10);
|
||||
const password = this.helperHashService.bcrypt(
|
||||
const salt = this.helperHashService.randomSalt(10); // Hash the password using bcrypt
|
||||
const hashedPassword = await this.helperHashService.bcrypt(
|
||||
userSignUpDto.password,
|
||||
salt,
|
||||
);
|
||||
return await this.userRepository.save({ ...userSignUpDto, password });
|
||||
|
||||
try {
|
||||
const user = await this.userRepository.save({
|
||||
...userSignUpDto,
|
||||
password: hashedPassword,
|
||||
});
|
||||
|
||||
const defaultUserRoleUuid = await this.getRoleUuidByRoleType(
|
||||
RoleType.USER,
|
||||
);
|
||||
|
||||
await this.userRoleRepository.save({
|
||||
user: { uuid: user.uuid },
|
||||
roleType: { uuid: defaultUserRoleUuid },
|
||||
});
|
||||
|
||||
return user;
|
||||
} catch (error) {
|
||||
throw new BadRequestException('Failed to register user');
|
||||
}
|
||||
}
|
||||
|
||||
async findUser(email: string) {
|
||||
@ -67,6 +91,7 @@ export class UserAuthService {
|
||||
|
||||
async userLogin(data: UserLoginDto) {
|
||||
const user = await this.authService.validateUser(data.email, data.password);
|
||||
|
||||
if (!user) {
|
||||
throw new UnauthorizedException('Invalid login credentials.');
|
||||
}
|
||||
@ -89,6 +114,9 @@ export class UserAuthService {
|
||||
email: user.email,
|
||||
userId: user.uuid,
|
||||
uuid: user.uuid,
|
||||
roles: user.role.map((role) => {
|
||||
return { uuid: role.uuid, type: role.roleType.type };
|
||||
}),
|
||||
sessionId: session[1].uuid,
|
||||
});
|
||||
}
|
||||
@ -186,4 +214,11 @@ export class UserAuthService {
|
||||
await this.authService.updateRefreshToken(user.uuid, tokens.refreshToken);
|
||||
return tokens;
|
||||
}
|
||||
private async getRoleUuidByRoleType(roleType: string) {
|
||||
const role = await this.roleTypeRepository.findOne({
|
||||
where: { type: roleType },
|
||||
});
|
||||
|
||||
return role.uuid;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user