nest-project/src/auth/auth.controller.ts

35 lines
1.0 KiB
TypeScript
Raw Normal View History

2023-02-16 15:43:38 +08:00
import { Body, Controller, Get, Post } 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-16 17:10:03 +08:00
import { ApiTags } from '@nestjs/swagger'
2023-02-16 01:18:55 +08:00
import { LoginInputDto } from './dto/login-input.dto'
import { Token } from 'src/common/decorators/token.decorator'
2023-02-16 17:25:13 +08:00
import { NeedAuth } from 'src/common/decorators/need-auth.decorator'
import { UsersService } from 'src/users/users.service'
2023-02-16 01:18:55 +08:00
@ApiTags('auth')
@Controller()
export class AuthController {
constructor(
private readonly authService: AuthService,
private readonly userService: UsersService,
) {}
2023-02-16 01:18:55 +08:00
2023-02-16 10:40:03 +08:00
@Post('api/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
}
@Post('api/login')
async login(@Body() user: LoginInputDto) {
return this.authService.login(user.email, user.password)
}
2023-02-16 12:09:48 +08:00
2023-02-16 17:10:03 +08:00
@NeedAuth()
2023-02-16 12:09:48 +08:00
@Get('api/profile')
async getUserInfo(@Token('userId') userId: string) {
const user = await this.userService.findUser({ id: userId })
2023-02-16 15:06:51 +08:00
return user
2023-02-16 12:09:48 +08:00
}
2023-02-16 01:18:55 +08:00
}