61 lines
2.0 KiB
TypeScript
61 lines
2.0 KiB
TypeScript
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, IsMobile } 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';
|
||
import { AuthUser } from './decorators/auth-user.decorator';
|
||
import { ApiSecurityAuth } from '~/common/decorators/swagger.decorator';
|
||
import { Domain, SkDomain } from '~/common/decorators/domain.decorator';
|
||
|
||
@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,
|
||
@IsMobile() isMobile: boolean,
|
||
@Headers('user-agent') ua: string
|
||
): Promise<LoginToken> {
|
||
if (!isMobile) {
|
||
await this.captchaService.checkImgCaptcha(dto.captchaId, dto.verifyCode);
|
||
}
|
||
const token = await this.authService.login(dto.username, dto.password, ip, ua);
|
||
return { token };
|
||
}
|
||
|
||
@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;
|
||
}
|
||
|
||
@Post('register')
|
||
@ApiOperation({ summary: '注册' })
|
||
async register(@Domain() domain: SkDomain, @Body() dto: RegisterDto): Promise<void> {
|
||
await this.userService.register(dto, domain);
|
||
}
|
||
}
|