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

61 lines
2.0 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';
2024-03-21 15:03:07 +08:00
import { Ip, IsMobile } 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-03-01 16:00:42 +08:00
import { AuthUser } from './decorators/auth-user.decorator';
import { ApiSecurityAuth } from '~/common/decorators/swagger.decorator';
2024-04-16 14:06:02 +08:00
import { Domain, SkDomain } from '~/common/decorators/domain.decorator';
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,
2024-03-21 15:03:07 +08:00
@IsMobile() isMobile: boolean,
2024-02-28 17:02:46 +08:00
@Headers('user-agent') ua: string
): Promise<LoginToken> {
2024-04-16 14:06:02 +08:00
if (!isMobile) {
2024-03-21 15:03:07 +08:00
await this.captchaService.checkImgCaptcha(dto.captchaId, dto.verifyCode);
}
2024-02-28 17:02:46 +08:00
const token = await this.authService.login(dto.username, dto.password, ip, ua);
return { token };
2024-02-28 08:32:35 +08:00
}
2024-03-01 16:00:42 +08:00
@Post('unlock')
@ApiSecurityAuth()
@ApiOperation({ summary: '屏幕解锁使用密码和token' })
@ApiResult({ type: LoginToken })
async unlock(@Body() dto: LoginDto, @AuthUser() user: IAuthUser): Promise<Boolean> {
await this.authService.unlock(user.uid, dto.password);
return true;
}
2024-02-28 08:32:35 +08:00
@Post('register')
@ApiOperation({ summary: '注册' })
2024-04-16 14:06:02 +08:00
async register(@Domain() domain: SkDomain, @Body() dto: RegisterDto): Promise<void> {
await this.userService.register(dto, domain);
2024-02-28 08:32:35 +08:00
}
}