32 lines
1.1 KiB
TypeScript
32 lines
1.1 KiB
TypeScript
import { Body, Controller, Post, Put } from '@nestjs/common'
|
||
import { AuthService } from './auth.service'
|
||
import { CreateUserDto } from 'src/users/dto/create-user.dto'
|
||
import { ApiTags, ApiOperation, ApiUnauthorizedResponse } from '@nestjs/swagger'
|
||
import { LoginInputDto } from './dto/login-input.dto'
|
||
import { TokenRefreshPayload } from './dto/token.dto'
|
||
|
||
@ApiTags('Auth')
|
||
@Controller('api/auth')
|
||
export class AuthController {
|
||
constructor(private readonly authService: AuthService) {}
|
||
|
||
@ApiOperation({ summary: '注册用户,返回token' })
|
||
@Post('register')
|
||
async register(@Body() userData: CreateUserDto) {
|
||
return this.authService.register(userData)
|
||
}
|
||
|
||
@ApiOperation({ summary: '登录用户,返回token' })
|
||
@Post('login')
|
||
async login(@Body() user: LoginInputDto) {
|
||
return this.authService.login(user.email, user.password)
|
||
}
|
||
|
||
@ApiOperation({ summary: '刷新token' })
|
||
@ApiUnauthorizedResponse({ description: 'Unauthorized' })
|
||
@Put('token')
|
||
async refreshToken(@Body() payload: TokenRefreshPayload) {
|
||
return this.authService.refreshToken(payload.refreshToken)
|
||
}
|
||
}
|