157 lines
4.1 KiB
TypeScript
157 lines
4.1 KiB
TypeScript
import { Body, Controller, HttpCode, HttpStatus, Post } from '@nestjs/common';
|
|
import { LoginDTO } from './dtos/login.dto';
|
|
import { AuthService } from './providers/auth.service';
|
|
import { VerifyOtpDTO } from './dtos/verify-otp.dto';
|
|
import { Public } from './decorators/public.decorator';
|
|
import { RefreshTokenDTO } from './dtos/refresh-token.dto';
|
|
import { Throttle } from '@nestjs/throttler';
|
|
import { ApiAppResponse } from '@/common/decorators/api-app-response.decorator';
|
|
import { NewUserLoginResponseDTO } from './dtos/responses/new-user-login.dto';
|
|
import { RequirePasswordDTO } from './dtos/responses/require-password.dto';
|
|
import { TokensResponseDTO } from './dtos/responses/tokens.dto';
|
|
import { LogoutDTO } from './dtos/logout.dto';
|
|
import { ActiveUser } from '@/common/decorators/active-user.decorator';
|
|
import { User } from '@/modules/users/entities/user.entity';
|
|
|
|
@Controller('auth')
|
|
export class AuthController {
|
|
constructor(
|
|
/**
|
|
* Inject Auth Service
|
|
*/
|
|
private readonly authService: AuthService,
|
|
) {}
|
|
|
|
@Public()
|
|
@Post('login')
|
|
@HttpCode(HttpStatus.OK)
|
|
@Throttle({ login: {} })
|
|
@ApiAppResponse([
|
|
{
|
|
status: HttpStatus.OK,
|
|
variants: [
|
|
{
|
|
model: TokensResponseDTO,
|
|
example: {
|
|
data: {
|
|
access: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...',
|
|
refresh: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...',
|
|
},
|
|
},
|
|
},
|
|
{
|
|
model: NewUserLoginResponseDTO,
|
|
example: {
|
|
data: {
|
|
newUser: true,
|
|
otpExpiresAt: '2026-09-01T09:24:34.894Z',
|
|
},
|
|
message: 'کد تایید به شماره 09121111111 فرستاده شد.',
|
|
},
|
|
},
|
|
{
|
|
model: RequirePasswordDTO,
|
|
example: {
|
|
data: {
|
|
newUser: false,
|
|
},
|
|
message: 'لطفا رمز عبور را ارسال کنید.',
|
|
},
|
|
},
|
|
],
|
|
},
|
|
{
|
|
status: HttpStatus.UNAUTHORIZED,
|
|
variants: [
|
|
{
|
|
messageExample: 'شماره موبایل یا رمز عبور اشتباه است.',
|
|
},
|
|
],
|
|
},
|
|
{
|
|
status: HttpStatus.TOO_MANY_REQUESTS,
|
|
variants: [
|
|
{
|
|
messageExample:
|
|
'تعداد درخواست ها بیشتر از حد مجاز است! لطفا بعد از 1 دقیقه دوباره امتحان کنید.',
|
|
},
|
|
],
|
|
},
|
|
])
|
|
public async logIn(@Body() loginDto: LoginDTO) {
|
|
return await this.authService.logIn(loginDto);
|
|
}
|
|
|
|
@Public()
|
|
@Post('verify-otp')
|
|
@HttpCode(HttpStatus.OK)
|
|
@Throttle({ otp: {} })
|
|
@ApiAppResponse([
|
|
{
|
|
status: HttpStatus.OK,
|
|
variants: [
|
|
{
|
|
model: TokensResponseDTO,
|
|
},
|
|
],
|
|
},
|
|
{
|
|
status: HttpStatus.UNAUTHORIZED,
|
|
variants: [
|
|
{
|
|
messageExample: 'کد تایید اشتباه است یا منقضی شده.',
|
|
},
|
|
],
|
|
},
|
|
])
|
|
public async verifyOTP(@Body() verifyOtpDto: VerifyOtpDTO) {
|
|
return await this.authService.verifyOTP(verifyOtpDto);
|
|
}
|
|
|
|
@Public()
|
|
@Post('refresh')
|
|
@HttpCode(HttpStatus.OK)
|
|
@Throttle({ login: {} })
|
|
@ApiAppResponse([
|
|
{
|
|
status: HttpStatus.OK,
|
|
variants: [
|
|
{
|
|
model: TokensResponseDTO,
|
|
},
|
|
],
|
|
},
|
|
{
|
|
status: HttpStatus.UNAUTHORIZED,
|
|
variants: [{ messageExample: 'توکن قبلا استفاده شده.' }],
|
|
},
|
|
])
|
|
public async refreshToken(@Body() refreshTokenDto: RefreshTokenDTO) {
|
|
return await this.authService.refreshToken(refreshTokenDto);
|
|
}
|
|
|
|
@Post('logout')
|
|
@HttpCode(HttpStatus.OK)
|
|
@ApiAppResponse([
|
|
{
|
|
status: HttpStatus.OK,
|
|
variants: [
|
|
{
|
|
messageExample: 'با موفقیت خارج شدید.',
|
|
},
|
|
],
|
|
},
|
|
{
|
|
status: HttpStatus.UNAUTHORIZED,
|
|
variants: [
|
|
{
|
|
messageExample: 'دسترسی غیر مجاز',
|
|
},
|
|
],
|
|
},
|
|
])
|
|
public async logout(@ActiveUser() user: User, @Body() logoutDto: LogoutDTO) {
|
|
return await this.authService.logout(user.id, logoutDto);
|
|
}
|
|
}
|