SPRINT-1 related tasks done

This commit is contained in:
VirajBrainvire
2024-04-17 18:52:58 +05:30
parent 34fcacde13
commit fbdf187ee1
40 changed files with 588 additions and 19 deletions

View File

@ -1,6 +1,7 @@
import { UserRepository } from '../../../libs/common/src/modules/user/repositories';
import {
BadRequestException,
ForbiddenException,
Injectable,
UnauthorizedException,
} from '@nestjs/common';
@ -14,7 +15,7 @@ import { ForgetPasswordDto, UserOtpDto, VerifyOtpDto } from '../dtos';
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 { ILoginResponse } from '../constants/login.response.constant';
import * as argon2 from 'argon2';
@Injectable()
export class UserAuthService {
@ -64,7 +65,7 @@ export class UserAuthService {
);
}
async userLogin(data: UserLoginDto): Promise<ILoginResponse> {
async userLogin(data: UserLoginDto) {
const user = await this.authService.validateUser(data.email, data.password);
if (!user) {
throw new UnauthorizedException('Invalid login credentials.');
@ -86,7 +87,7 @@ export class UserAuthService {
return await this.authService.login({
email: user.email,
userId: user.id,
userId: user.uuid,
uuid: user.uuid,
sessionId: session[1].uuid,
});
@ -97,7 +98,7 @@ export class UserAuthService {
if (!user) {
throw new BadRequestException('User does not found');
}
return await this.userRepository.delete({ uuid });
return await this.userRepository.update({ uuid }, { isActive: false });
}
async findOneById(id: string): Promise<UserEntity> {
@ -148,4 +149,41 @@ export class UserAuthService {
return true;
}
async userList(): Promise<UserEntity[]> {
return await this.userRepository.find({
where: { isActive: true },
select: {
firstName: true,
lastName: true,
email: true,
isActive: true,
},
});
}
async refreshToken(
userId: string,
refreshToken: string,
type: string,
sessionId: string,
) {
const user = await this.userRepository.findOne({ where: { uuid: userId } });
if (!user || !user.refreshToken)
throw new ForbiddenException('Access Denied');
const refreshTokenMatches = await argon2.verify(
user.refreshToken,
refreshToken,
);
if (!refreshTokenMatches) throw new ForbiddenException('Access Denied');
const tokens = await this.authService.getTokens({
email: user.email,
userId: user.uuid,
uuid: user.uuid,
type,
sessionId,
});
await this.authService.updateRefreshToken(user.uuid, tokens.refreshToken);
return tokens;
}
}