nest-project/src/auth/auth.controller.ts
2023-02-20 16:27:25 +08:00

32 lines
1.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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)
}
}