mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-15 18:27:05 +00:00
162 lines
4.9 KiB
TypeScript
162 lines
4.9 KiB
TypeScript
import {
|
|
BadRequestException,
|
|
Body,
|
|
Controller,
|
|
Get,
|
|
HttpStatus,
|
|
Post,
|
|
Req,
|
|
UseGuards,
|
|
} from '@nestjs/common';
|
|
import { UserAuthService } from '../services/user-auth.service';
|
|
import { UserSignUpDto } from '../dtos/user-auth.dto';
|
|
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
|
|
import { ResponseMessage } from '../../../libs/common/src/response/response.decorator';
|
|
import { UserLoginDto } from '../dtos/user-login.dto';
|
|
import { ForgetPasswordDto, UserOtpDto, VerifyOtpDto } from '../dtos';
|
|
import { RefreshTokenGuard } from '@app/common/guards/jwt-refresh.auth.guard';
|
|
import { SuperAdminRoleGuard } from 'src/guards/super.admin.role.guard';
|
|
import { EnableDisableStatusEnum } from '@app/common/constants/days.enum';
|
|
import { OtpType } from '@app/common/constants/otp-type.enum';
|
|
import { ControllerRoute } from '@app/common/constants/controller-route';
|
|
|
|
@Controller({
|
|
version: EnableDisableStatusEnum.ENABLED,
|
|
path: ControllerRoute.AUTHENTICATION.ROUTE,
|
|
})
|
|
@ApiTags('Authentication Module')
|
|
export class UserAuthController {
|
|
constructor(private readonly userAuthService: UserAuthService) {}
|
|
|
|
@ResponseMessage('User Registered Successfully')
|
|
@Post('user/signup')
|
|
@ApiOperation({
|
|
summary: ControllerRoute.AUTHENTICATION.ACTIONS.SIGN_UP_SUMMARY,
|
|
description: ControllerRoute.AUTHENTICATION.ACTIONS.SIGN_UP_DESCRIPTION,
|
|
})
|
|
async signUp(@Body() userSignUpDto: UserSignUpDto) {
|
|
const signupUser = await this.userAuthService.signUp(userSignUpDto);
|
|
return {
|
|
statusCode: HttpStatus.CREATED,
|
|
data: {
|
|
id: signupUser.uuid,
|
|
default: () => 'gen_random_uuid()', // this is a default value for the uuid column
|
|
},
|
|
message: 'User Registered Successfully',
|
|
};
|
|
}
|
|
|
|
@ResponseMessage('User Logged in Successfully')
|
|
@Post('user/login')
|
|
@ApiOperation({
|
|
summary: ControllerRoute.AUTHENTICATION.ACTIONS.LOGIN_SUMMARY,
|
|
description: ControllerRoute.AUTHENTICATION.ACTIONS.LOGIN_DESCRIPTION,
|
|
})
|
|
async userLogin(@Body() data: UserLoginDto) {
|
|
const accessToken = await this.userAuthService.userLogin(data);
|
|
return {
|
|
statusCode: HttpStatus.CREATED,
|
|
data: accessToken,
|
|
message: 'User Logged in Successfully',
|
|
};
|
|
}
|
|
|
|
@Post('user/send-otp')
|
|
@ApiOperation({
|
|
summary: ControllerRoute.AUTHENTICATION.ACTIONS.SEND_OTP_SUMMARY,
|
|
description: ControllerRoute.AUTHENTICATION.ACTIONS.SEND_OTP_DESCRIPTION,
|
|
})
|
|
async sendOtp(@Body() otpDto: UserOtpDto) {
|
|
const otpCode = await this.userAuthService.generateOTP(otpDto);
|
|
return {
|
|
statusCode: HttpStatus.OK,
|
|
data: {
|
|
...otpCode,
|
|
},
|
|
message: 'Otp Sent Successfully',
|
|
};
|
|
}
|
|
|
|
@Post('user/verify-otp')
|
|
@ApiOperation({
|
|
summary: ControllerRoute.AUTHENTICATION.ACTIONS.VERIFY_OTP_SUMMARY,
|
|
description: ControllerRoute.AUTHENTICATION.ACTIONS.VERIFY_OTP_DESCRIPTION,
|
|
})
|
|
async verifyOtp(@Body() verifyOtpDto: VerifyOtpDto) {
|
|
await this.userAuthService.verifyOTP(verifyOtpDto);
|
|
return {
|
|
statusCode: HttpStatus.OK,
|
|
data: {},
|
|
message: 'Otp Verified Successfully',
|
|
};
|
|
}
|
|
|
|
@Post('user/forget-password')
|
|
@ApiOperation({
|
|
summary: ControllerRoute.AUTHENTICATION.ACTIONS.FORGET_PASSWORD_SUMMARY,
|
|
description:
|
|
ControllerRoute.AUTHENTICATION.ACTIONS.FORGET_PASSWORD_DESCRIPTION,
|
|
})
|
|
async forgetPassword(@Body() forgetPasswordDto: ForgetPasswordDto) {
|
|
const otpResult = await this.userAuthService.verifyOTP(
|
|
{
|
|
otpCode: forgetPasswordDto.otpCode,
|
|
email: forgetPasswordDto.email,
|
|
type: OtpType.PASSWORD,
|
|
},
|
|
true,
|
|
);
|
|
if (otpResult) {
|
|
await this.userAuthService.forgetPassword(forgetPasswordDto);
|
|
return {
|
|
statusCode: HttpStatus.OK,
|
|
data: {},
|
|
message: 'Password changed successfully',
|
|
};
|
|
}
|
|
throw new BadRequestException({
|
|
statusCode: HttpStatus.BAD_REQUEST,
|
|
data: {},
|
|
message: 'Otp is incorrect',
|
|
});
|
|
}
|
|
|
|
@ApiBearerAuth()
|
|
@UseGuards(SuperAdminRoleGuard)
|
|
@Get('user')
|
|
@ApiOperation({
|
|
summary: ControllerRoute.AUTHENTICATION.ACTIONS.USER_LIST_SUMMARY,
|
|
description: ControllerRoute.AUTHENTICATION.ACTIONS.USER_LIST_DESCRIPTION,
|
|
})
|
|
async userList() {
|
|
const userList = await this.userAuthService.userList();
|
|
return {
|
|
statusCode: HttpStatus.OK,
|
|
data: userList,
|
|
message: 'User List Fetched Successfully',
|
|
};
|
|
}
|
|
|
|
@ApiBearerAuth()
|
|
@UseGuards(RefreshTokenGuard)
|
|
@Get('refresh-token')
|
|
@ApiOperation({
|
|
summary: ControllerRoute.AUTHENTICATION.ACTIONS.REFRESH_TOKEN_SUMMARY,
|
|
description:
|
|
ControllerRoute.AUTHENTICATION.ACTIONS.REFRESH_TOKEN_DESCRIPTION,
|
|
})
|
|
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',
|
|
};
|
|
}
|
|
}
|