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

@ -2,9 +2,11 @@ import {
Body,
Controller,
Delete,
Get,
HttpStatus,
Param,
Post,
Req,
UseGuards,
} from '@nestjs/common';
import { UserAuthService } from '../services/user-auth.service';
@ -14,6 +16,8 @@ import { ResponseMessage } from '../../../libs/common/src/response/response.deco
import { UserLoginDto } from '../dtos/user-login.dto';
import { JwtAuthGuard } from '../../../libs/common/src/guards/jwt.auth.guard';
import { ForgetPasswordDto, UserOtpDto, VerifyOtpDto } from '../dtos';
import { Request } from 'express';
import { RefreshTokenGuard } from '@app/common/guards/jwt-refresh.auth.guard';
@Controller({
version: '1',
@ -93,4 +97,33 @@ export class UserAuthController {
message: 'Password changed successfully',
};
}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Get('user/list')
async userList(@Req() req) {
const userList = await this.userAuthService.userList();
return {
statusCode: HttpStatus.OK,
data: userList,
message: 'User List Fetched Successfully',
};
}
@ApiBearerAuth()
@UseGuards(RefreshTokenGuard)
@Get('refresh-token')
async refreshToken(@Req() req) {
const refreshToken = await this.userAuthService.refreshToken(
req.user.uuid,
req.headers.authorization,
req.user.type,
req.user.sessionId,
);
return {
statusCode: HttpStatus.OK,
data: refreshToken,
message: 'Refresh Token added Successfully',
};
}
}

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;
}
}