|
| 1 | +import { AuthenticationService } from './authentication.service' |
| 2 | +import { Body, Controller, Get, Post, Req, UseGuards } from '@nestjs/common' |
| 3 | +import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger' |
| 4 | +import { ResponseUtil } from 'src/utils/response' |
| 5 | +import { JwtAuthGuard } from './jwt.auth.guard' |
| 6 | +import { BindUsernameDto } from './dto/bind-username.dto' |
| 7 | +import { IRequest } from 'src/utils/interface' |
| 8 | +import { BindPhoneDto } from './dto/bind-phone.dto' |
| 9 | +import { SmsService } from './phone/sms.service' |
| 10 | +import { SmsVerifyCodeType } from '@prisma/client' |
| 11 | +import { UserService } from 'src/user/user.service' |
| 12 | + |
| 13 | +@ApiTags('Authentication - New') |
| 14 | +@Controller('auth') |
| 15 | +export class AuthenticationController { |
| 16 | + constructor( |
| 17 | + private readonly authenticationService: AuthenticationService, |
| 18 | + private readonly smsService: SmsService, |
| 19 | + private readonly userService: UserService, |
| 20 | + ) {} |
| 21 | + |
| 22 | + /** |
| 23 | + * Auth providers |
| 24 | + */ |
| 25 | + @ApiOperation({ summary: 'Auth providers' }) |
| 26 | + @ApiResponse({ type: ResponseUtil }) |
| 27 | + @Get('providers') |
| 28 | + async getProviders() { |
| 29 | + const providers = await this.authenticationService.getProviders() |
| 30 | + return ResponseUtil.ok(providers) |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Bind phone |
| 35 | + */ |
| 36 | + @ApiOperation({ summary: 'Bind username' }) |
| 37 | + @ApiResponse({ type: ResponseUtil }) |
| 38 | + @UseGuards(JwtAuthGuard) |
| 39 | + @Post('bind/phone') |
| 40 | + async bindPhone(@Body() dto: BindPhoneDto, @Req() req: IRequest) { |
| 41 | + const { phone, code } = dto |
| 42 | + // check code valid |
| 43 | + const err = await this.smsService.validCode( |
| 44 | + phone, |
| 45 | + code, |
| 46 | + SmsVerifyCodeType.Bind, |
| 47 | + ) |
| 48 | + if (err) { |
| 49 | + return ResponseUtil.error(err) |
| 50 | + } |
| 51 | + |
| 52 | + // check phone if have already been bound |
| 53 | + const user = await this.userService.find(phone) |
| 54 | + if (user) { |
| 55 | + return ResponseUtil.error('phone already been bound') |
| 56 | + } |
| 57 | + |
| 58 | + // bind phone |
| 59 | + await this.userService.updateUser({ |
| 60 | + where: { |
| 61 | + id: req.user.id, |
| 62 | + }, |
| 63 | + data: { |
| 64 | + phone, |
| 65 | + }, |
| 66 | + }) |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Bind username, not support bind existed username |
| 71 | + */ |
| 72 | + @ApiOperation({ summary: 'Bind username' }) |
| 73 | + @ApiResponse({ type: ResponseUtil }) |
| 74 | + @UseGuards(JwtAuthGuard) |
| 75 | + @Post('bind/username') |
| 76 | + async bindUsername(@Body() dto: BindUsernameDto, @Req() req: IRequest) { |
| 77 | + const { username, phone, code } = dto |
| 78 | + |
| 79 | + // check code valid |
| 80 | + const err = await this.smsService.validCode( |
| 81 | + phone, |
| 82 | + code, |
| 83 | + SmsVerifyCodeType.Bind, |
| 84 | + ) |
| 85 | + if (err) { |
| 86 | + return ResponseUtil.error(err) |
| 87 | + } |
| 88 | + |
| 89 | + // check username if have already been bound |
| 90 | + const user = await this.userService.find(username) |
| 91 | + if (user) { |
| 92 | + return ResponseUtil.error('username already been bound') |
| 93 | + } |
| 94 | + |
| 95 | + // bind username |
| 96 | + await this.userService.updateUser({ |
| 97 | + where: { |
| 98 | + id: req.user.id, |
| 99 | + }, |
| 100 | + data: { |
| 101 | + username, |
| 102 | + }, |
| 103 | + }) |
| 104 | + } |
| 105 | +} |
0 commit comments