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

46 lines
1.4 KiB
TypeScript
Raw Normal View History

2024-02-28 17:02:46 +08:00
import { Body, Controller, Headers, Post, UseGuards } from '@nestjs/common';
import { ApiOperation, ApiTags } from '@nestjs/swagger';
2024-02-28 08:32:35 +08:00
2024-02-28 17:02:46 +08:00
import { ApiResult } from '~/common/decorators/api-result.decorator';
import { Ip } from '~/common/decorators/http.decorator';
2024-02-28 08:32:35 +08:00
2024-02-28 17:02:46 +08:00
import { UserService } from '../user/user.service';
2024-02-28 08:32:35 +08:00
2024-02-28 17:02:46 +08:00
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';
2024-02-28 08:32:35 +08:00
@ApiTags('Auth - 认证模块')
@UseGuards(LocalGuard)
@Public()
@Controller('auth')
export class AuthController {
constructor(
private authService: AuthService,
private userService: UserService,
2024-02-28 17:02:46 +08:00
private captchaService: CaptchaService
2024-02-28 08:32:35 +08:00
) {}
@Post('login')
@ApiOperation({ summary: '登录' })
@ApiResult({ type: LoginToken })
async login(
2024-02-28 17:02:46 +08:00
@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 };
2024-02-28 08:32:35 +08:00
}
@Post('register')
@ApiOperation({ summary: '注册' })
async register(@Body() dto: RegisterDto): Promise<void> {
2024-02-28 17:02:46 +08:00
await this.userService.register(dto);
2024-02-28 08:32:35 +08:00
}
}