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