import { Body, Controller, HttpCode, HttpStatus, Patch, Post } from "@nestjs/common"; import { ApiOperation, ApiTags } from "@nestjs/swagger"; import { ChangePasswordDto } from "./DTO/change-password.dto"; import { CheckUserExistDto, LoginPasswordDTO } from "./DTO/loginPassword.dto"; import { RefreshTokenDto } from "./DTO/refresh-token.dto"; import { RequestOtpDto } from "./DTO/request-otp.dto"; import { SSOTokenRequestDTO } from "./DTO/requests/sso-token-request.dto"; import { SSOTokenValidateDTO } from "./DTO/requests/sso-token-validate.dto"; import { VerifyOtpDto } from "./DTO/verify-otp.dto"; import { AuthService } from "./providers/auth.service"; import { SSOService } from "./providers/sso.service"; import { AuthGuards } from "../../common/decorators/auth-guard.decorator"; import { RefreshTokenRateLimit, StrictRateLimit } from "../../common/decorators/rate-limit.decorator"; import { UserDec } from "../../common/decorators/user.decorator"; import { User } from "../users/entities/user.entity"; import { ForgetPasswordDto } from "./DTO/forget-password.dto"; import { VerifyForgotPasswordOtpDto } from "./DTO/verify-forgot-otp.dto"; @ApiTags("Auth") @Controller("auth") @StrictRateLimit() export class AuthController { constructor( private readonly authService: AuthService, private readonly ssoService: SSOService, ) { } @ApiOperation({ summary: "request to login with otp" }) @HttpCode(HttpStatus.OK) @Post("otp/send") sendOtp(@Body() requestOtpDto: RequestOtpDto) { return this.authService.requestLoginOtp(requestOtpDto); } @ApiOperation({ summary: "request to login with otp as reseller" }) @HttpCode(HttpStatus.OK) @Post("otp/send/reseller") sendOtpReseller(@Body() requestOtpDto: RequestOtpDto) { return this.authService.requestLoginOtpReseller(requestOtpDto); } @ApiOperation({ summary: "verify otp for login" }) @HttpCode(HttpStatus.OK) @Post("otp/verify") verifyOtp(@Body() verifyOtpDto: VerifyOtpDto) { return this.authService.verifyLoginOtp(verifyOtpDto); } @ApiOperation({ summary: "check if user email exist" }) @HttpCode(HttpStatus.OK) @Post("check") checkUserExist(@Body() checkUserExistDto: CheckUserExistDto) { return this.authService.checkUserExist(checkUserExistDto); } @ApiOperation({ summary: "login with password" }) @HttpCode(HttpStatus.OK) @Post("login/password") loginWithPassword(@Body() loginPasswordDto: LoginPasswordDTO) { return this.authService.loginWithPassword(loginPasswordDto); } //***************************** */ @ApiOperation({ summary: "login with password ==> admin" }) @HttpCode(HttpStatus.OK) @Post("login/password/admin") adminLoginWithPassword(@Body() loginPasswordDto: LoginPasswordDTO) { return this.authService.adminLoginWithPassword(loginPasswordDto); } @ApiOperation({ summary: "verify otp for login" }) @HttpCode(HttpStatus.OK) @Post("otp/verify/admin") adminVerifyOtp(@Body() verifyOtpDto: VerifyOtpDto) { return this.authService.adminVerifyLoginOtp(verifyOtpDto); } @ApiOperation({ summary: "verify otp for login" }) @HttpCode(HttpStatus.OK) @Post("otp/verify/reseller") resellerVerifyOtp(@Body() verifyOtpDto: VerifyOtpDto) { return this.authService.resellerVerifyLoginOtp(verifyOtpDto); } @AuthGuards() @ApiOperation({ summary: "Update user password" }) @HttpCode(HttpStatus.OK) @Patch("change-password") changePassword(@UserDec("id") userId: string, @Body() changePasswordDto: ChangePasswordDto) { return this.authService.changePassword(userId, changePasswordDto); } @RefreshTokenRateLimit() @ApiOperation({ summary: "refresh the user access token / refresh token" }) @HttpCode(HttpStatus.OK) @Post("refresh") refresh(@Body() refreshTokenDto: RefreshTokenDto) { return this.authService.refreshToken(refreshTokenDto.refreshToken); } @AuthGuards() @ApiOperation({ summary: "logout the user" }) @HttpCode(HttpStatus.OK) @Post("logout") logout(@UserDec("id") userId: string) { return this.authService.logout(userId); } //***************************** */ @ApiOperation({ summary: "Generate otp and send vial email" }) @Post("forgot-password") async forgotPassword(@Body() dto: ForgetPasswordDto) { return this.authService.requestForgotPasswordOtp(dto); } @ApiOperation({ summary: "verify otp and update password" }) @Post("reset-password") async resetPassword(@Body() resetPasswordDto: VerifyForgotPasswordOtpDto) { return this.authService.verifyForgotPasswordOtp(resetPasswordDto); } @ApiOperation({ summary: "Generate SSO token for client application" }) @HttpCode(HttpStatus.OK) @Post("/sso/token") async generateSSOToken(@UserDec() user: User, @Body() body: SSOTokenRequestDTO) { const { clientId } = body; const ssoToken = await this.ssoService.generateSSOToken(user, clientId); return { statusCode: HttpStatus.OK, data: ssoToken, message: "SSO token generated successfully", }; } @ApiOperation({ summary: "Validate SSO token" }) @HttpCode(HttpStatus.OK) @Post("/sso/validate") async validateSSOToken(@Body() body: SSOTokenValidateDTO) { const { token, clientId } = body; const payload = await this.ssoService.validateSSOToken(token, clientId); return { statusCode: HttpStatus.OK, data: payload, message: "SSO token validated successfully", }; } }