oa_based/src/modules/auth/auth.controller.ts

48 lines
1.4 KiB
TypeScript
Raw Normal View History

2024-02-28 08:32:35 +08:00
import { Body, Controller, Headers, Post, UseGuards } from '@nestjs/common'
import { ApiOperation, ApiTags } from '@nestjs/swagger'
import { ApiResult } from '~/common/decorators/api-result.decorator'
import { Ip } from '~/common/decorators/http.decorator'
import { UserService } from '../user/user.service'
import { AuthService } from './auth.service'
import { Public } from './decorators/public.decorator'
import { LoginDto, RegisterDto } from './dto/auth.dto'
import { LocalGuard } from './guards/local.guard'
import { LoginToken } from './models/auth.model'
import { CaptchaService } from './services/captcha.service'
@ApiTags('Auth - 认证模块')
@UseGuards(LocalGuard)
@Public()
@Controller('auth')
export class AuthController {
constructor(
private authService: AuthService,
private userService: UserService,
private captchaService: CaptchaService,
) {}
@Post('login')
@ApiOperation({ summary: '登录' })
@ApiResult({ type: LoginToken })
async login(
@Body() dto: LoginDto, @Ip() ip: string, @Headers('user-agent') ua: string): Promise<LoginToken> {
await this.captchaService.checkImgCaptcha(dto.captchaId, dto.verifyCode)
const token = await this.authService.login(
dto.username,
dto.password,
ip,
ua,
)
return { token }
}
@Post('register')
@ApiOperation({ summary: '注册' })
async register(@Body() dto: RegisterDto): Promise<void> {
await this.userService.register(dto)
}
}