Skip to content

Commit 6d5882c

Browse files
authored
fix(server): no need to validate old phone when phone num miss (#1507)
1 parent 1582081 commit 6d5882c

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

server/src/user/user.controller.ts

+20-13
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import { FileInterceptor } from '@nestjs/platform-express'
2222
import { IRequest, IResponse } from 'src/utils/interface'
2323
import { ApiResponseObject, ResponseUtil } from 'src/utils/response'
2424
import { JwtAuthGuard } from 'src/authentication/jwt.auth.guard'
25-
import { UserWithProfile } from './entities/user'
25+
import { User, UserWithProfile } from './entities/user'
2626
import { SmsService } from 'src/authentication/phone/sms.service'
2727
import { BindPhoneDto } from './dto/bind-phone.dto'
2828
import { SmsVerifyCodeType } from 'src/authentication/entities/sms-verify-code'
@@ -108,18 +108,25 @@ export class UserController {
108108
@ApiResponseObject(UserWithProfile)
109109
@UseGuards(JwtAuthGuard)
110110
@Post('bind/phone')
111-
async bindPhone(@Body() dto: BindPhoneDto, @Req() req: IRequest) {
111+
async bindPhone(@Body() dto: BindPhoneDto, @Req() user: User) {
112112
const { oldPhoneNumber, newPhoneNumber, oldSmsCode, newSmsCode } = dto
113113
// check code valid
114-
let err = await this.smsService.validateCode(
115-
oldPhoneNumber,
116-
oldSmsCode,
117-
SmsVerifyCodeType.Unbind,
118-
)
119-
if (err) {
120-
return ResponseUtil.error(err)
114+
if (user.phone) {
115+
if (user.phone !== dto.oldPhoneNumber) {
116+
return ResponseUtil.error(
117+
'the old phone number is not the same as the new one',
118+
)
119+
}
120+
const err = await this.smsService.validateCode(
121+
oldPhoneNumber,
122+
oldSmsCode,
123+
SmsVerifyCodeType.Unbind,
124+
)
125+
if (err) {
126+
return ResponseUtil.error(err)
127+
}
121128
}
122-
err = await this.smsService.validateCode(
129+
const err = await this.smsService.validateCode(
123130
newPhoneNumber,
124131
newSmsCode,
125132
SmsVerifyCodeType.Bind,
@@ -129,13 +136,13 @@ export class UserController {
129136
}
130137

131138
// check phone if have already been bound
132-
const user = await this.userService.findOneByPhone(newPhoneNumber)
133-
if (user) {
139+
const _user = await this.userService.findOneByPhone(newPhoneNumber)
140+
if (_user) {
134141
return ResponseUtil.error('phone has already been bound')
135142
}
136143

137144
// bind phone
138-
const res = await this.userService.updateUser(req.user._id, {
145+
const res = await this.userService.updateUser(user._id, {
139146
phone: newPhoneNumber,
140147
})
141148
return res

0 commit comments

Comments
 (0)