auth.controller.ts 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import { PhoneLoginDto } from './dto/login.dto'
  2. import { Body, Controller, Get, Param, Post } from '@nestjs/common'
  3. import { AuthService } from './auth.service'
  4. import { ApiTags } from '@nestjs/swagger'
  5. import { Public } from './public.decorator'
  6. import { HasRoles } from './roles.decorator'
  7. import { Role } from '../model/role.enum'
  8. import { UserRegisterDto } from 'src/users/dto/user-register.dto'
  9. @ApiTags('auth')
  10. @Controller('/auth')
  11. export class AuthController {
  12. constructor(private readonly authService: AuthService) {}
  13. @Public()
  14. @Post('/phoneLogin')
  15. async phoneLogin(@Body() loginDto: PhoneLoginDto) {
  16. return await this.authService.loginByPhone(loginDto)
  17. }
  18. @Public()
  19. @Post('/login')
  20. async login(@Body() { username, password }) {
  21. return await this.authService.login(username, password)
  22. }
  23. @Public()
  24. @Post('/admin/login')
  25. async loginAdmin(@Body() { username, password }) {
  26. return await this.authService.loginAdmin(username, password)
  27. }
  28. @Get('/admin/user/:userId/token')
  29. @HasRoles(Role.Admin)
  30. async getToken(@Param('userId') userId: string) {
  31. return await this.authService.getToken(Number(userId))
  32. }
  33. @Public()
  34. @Post('/register')
  35. async register(@Body() register: UserRegisterDto) {
  36. return await this.authService.register(register)
  37. }
  38. }