@@ -26,6 +26,7 @@ import { GithubDto } from './dto/github.dto';
2626import { LoginByEmailDto , LoginByPhoneDto , LoginDto , LogoutDto } from './dto/login.dto' ;
2727import { RefreshTokenDto } from './dto/refresh-token.dto' ;
2828import { RegisterByEmailDto , RegisterbyPhoneDto , RegisterDto } from './dto/register.dto' ;
29+ import { ResetPasswordByEmailDto , ResetPasswordByPhoneDto } from './dto/reset-password.dto' ;
2930import { SignTokenDto } from './dto/sign-token.dto' ;
3031import { SessionWithToken , Token } from './entities/session-with-token.entity' ;
3132
@@ -424,4 +425,54 @@ export class AuthController {
424425 tokenExpireAt,
425426 } ;
426427 }
428+
429+ /**
430+ * Reset password by phone
431+ */
432+ @ApiOperation ( { operationId : 'resetPasswordByPhone' } )
433+ @HttpCode ( HttpStatus . NO_CONTENT )
434+ @Post ( '@resetPasswordByPhone' )
435+ async resetPasswordByPhone ( @Body ( ) dto : ResetPasswordByPhoneDto ) : Promise < void > {
436+ const user = await this . userService . findByPhone ( dto . phone ) ;
437+ if ( ! user ) {
438+ throw new NotFoundException ( {
439+ code : ErrorCodes . USER_NOT_FOUND ,
440+ message : `User with phone ${ dto . phone } not found.` ,
441+ } ) ;
442+ }
443+
444+ if ( ! ( await this . captchaService . consume ( dto . key , dto . code ) ) ) {
445+ throw new BadRequestException ( {
446+ code : ErrorCodes . CAPTCHA_INVALID ,
447+ message : 'captcha invalid.' ,
448+ } ) ;
449+ }
450+
451+ await this . userService . updatePassword ( user . id , dto . password ) ;
452+ }
453+
454+ /**
455+ * Reset password by email
456+ */
457+ @ApiOperation ( { operationId : 'resetPasswordByEmail' } )
458+ @HttpCode ( HttpStatus . NO_CONTENT )
459+ @Post ( '@resetPasswordByEmail' )
460+ async resetPasswordByEmail ( @Body ( ) dto : ResetPasswordByEmailDto ) : Promise < void > {
461+ const user = await this . userService . findByEmail ( dto . email ) ;
462+ if ( ! user ) {
463+ throw new NotFoundException ( {
464+ code : ErrorCodes . USER_NOT_FOUND ,
465+ message : `User with email ${ dto . email } not found.` ,
466+ } ) ;
467+ }
468+
469+ if ( ! ( await this . captchaService . consume ( dto . key , dto . code ) ) ) {
470+ throw new BadRequestException ( {
471+ code : ErrorCodes . CAPTCHA_INVALID ,
472+ message : 'captcha invalid.' ,
473+ } ) ;
474+ }
475+
476+ await this . userService . updatePassword ( user . id , dto . password ) ;
477+ }
427478}
0 commit comments