2023-02-20 16:25:46 +08:00
|
|
|
|
import { Body, Controller, Post, Put } from '@nestjs/common'
|
2023-02-16 01:18:55 +08:00
|
|
|
|
import { AuthService } from './auth.service'
|
|
|
|
|
import { CreateUserDto } from 'src/users/dto/create-user.dto'
|
2023-02-20 16:25:46 +08:00
|
|
|
|
import { ApiTags, ApiOperation, ApiUnauthorizedResponse } from '@nestjs/swagger'
|
2023-02-16 01:18:55 +08:00
|
|
|
|
import { LoginInputDto } from './dto/login-input.dto'
|
2023-02-20 16:25:46 +08:00
|
|
|
|
import { TokenRefreshPayload } from './dto/token.dto'
|
2023-02-16 01:18:55 +08:00
|
|
|
|
|
2023-02-17 14:21:08 +08:00
|
|
|
|
@ApiTags('Auth')
|
|
|
|
|
@Controller('api/auth')
|
2023-02-16 01:18:55 +08:00
|
|
|
|
export class AuthController {
|
2023-02-17 15:04:35 +08:00
|
|
|
|
constructor(private readonly authService: AuthService) {}
|
2023-02-16 01:18:55 +08:00
|
|
|
|
|
2023-02-20 16:25:46 +08:00
|
|
|
|
@ApiOperation({ summary: '注册用户,返回token' })
|
2023-02-17 14:21:08 +08:00
|
|
|
|
@Post('register')
|
2023-02-16 01:18:55 +08:00
|
|
|
|
async register(@Body() userData: CreateUserDto) {
|
2023-02-16 10:40:03 +08:00
|
|
|
|
return this.authService.register(userData)
|
2023-02-16 01:18:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-20 16:25:46 +08:00
|
|
|
|
@ApiOperation({ summary: '登录用户,返回token' })
|
2023-02-17 14:21:08 +08:00
|
|
|
|
@Post('login')
|
2023-02-16 01:18:55 +08:00
|
|
|
|
async login(@Body() user: LoginInputDto) {
|
|
|
|
|
return this.authService.login(user.email, user.password)
|
|
|
|
|
}
|
2023-02-17 15:56:35 +08:00
|
|
|
|
|
2023-02-20 16:25:46 +08:00
|
|
|
|
@ApiOperation({ summary: '刷新token' })
|
|
|
|
|
@ApiUnauthorizedResponse({ description: 'Unauthorized' })
|
|
|
|
|
@Put('token')
|
|
|
|
|
async refreshToken(@Body() payload: TokenRefreshPayload) {
|
2023-02-17 15:56:35 +08:00
|
|
|
|
return this.authService.refreshToken(payload.refreshToken)
|
|
|
|
|
}
|
2023-02-16 01:18:55 +08:00
|
|
|
|
}
|