51 lines
1.3 KiB
TypeScript
51 lines
1.3 KiB
TypeScript
import { Body, Controller, Get, Post, UseGuards } from '@nestjs/common';
|
|
import {
|
|
ApiBearerAuth,
|
|
ApiOperation,
|
|
ApiTags,
|
|
} from '@nestjs/swagger';
|
|
import { AuthService } from './auth.service.js';
|
|
import { RegisterUserDto } from '../users/dto/register-user.dto.js';
|
|
import { LoginDto } from '../users/dto/login.dto.js';
|
|
import { AccessTokenGuard } from './access-token.guard.js';
|
|
import { CurrentActor } from './current-actor.decorator.js';
|
|
import type { ActorContext } from '../common/actor-context.js';
|
|
|
|
/**
|
|
* 认证控制器:提供注册、登录、获取当前登录用户信息接口。
|
|
*/
|
|
@ApiTags('认证')
|
|
@Controller('auth')
|
|
export class AuthController {
|
|
constructor(private readonly authService: AuthService) {}
|
|
|
|
/**
|
|
* 注册账号。
|
|
*/
|
|
@Post('register')
|
|
@ApiOperation({ summary: '注册账号' })
|
|
register(@Body() dto: RegisterUserDto) {
|
|
return this.authService.register(dto);
|
|
}
|
|
|
|
/**
|
|
* 登录并换取 JWT。
|
|
*/
|
|
@Post('login')
|
|
@ApiOperation({ summary: '登录' })
|
|
login(@Body() dto: LoginDto) {
|
|
return this.authService.login(dto);
|
|
}
|
|
|
|
/**
|
|
* 获取当前登录用户信息。
|
|
*/
|
|
@Get('me')
|
|
@UseGuards(AccessTokenGuard)
|
|
@ApiBearerAuth('bearer')
|
|
@ApiOperation({ summary: '获取当前用户信息' })
|
|
me(@CurrentActor() actor: ActorContext) {
|
|
return this.authService.me(actor);
|
|
}
|
|
}
|