|
1 | 1 | import {Controller, Post, Body, Res, UseGuards, Logger} from '@nestjs/common'; |
2 | | -import { PasswdService } from './passwd.service'; |
| 2 | +import {PasswdService} from './passwd.service'; |
3 | 3 | import { |
4 | 4 | ApiTags, |
5 | 5 | ApiOperation, |
6 | 6 | ApiResponse, ApiBearerAuth |
7 | 7 | } from '@nestjs/swagger'; |
8 | | -import { Response } from 'express'; |
9 | | -import { ChangePasswordDto } from './dto/change-password.dto'; |
10 | | -import { AuthGuard } from '@nestjs/passport'; |
| 8 | +import {Response} from 'express'; |
| 9 | +import {ChangePasswordDto} from './dto/change-password.dto'; |
| 10 | +import {AuthGuard} from '@nestjs/passport'; |
| 11 | +import {AskTokenDto} from "./dto/ask-token.dto"; |
| 12 | +import {VerifyTokenDto} from "./dto/verify-token.dto"; |
| 13 | +import {ResetPasswordDto} from "./dto/reset-password.dto"; |
11 | 14 |
|
12 | 15 |
|
13 | 16 | @Controller('passwd') |
14 | 17 | @ApiTags('passwd') |
15 | 18 | export class PasswdController { |
16 | 19 | private readonly logger = new Logger(PasswdController.name); |
17 | | - constructor(private passwdService: PasswdService) { |
18 | | - } |
19 | | - @Post() |
20 | | - @ApiOperation({ summary: 'change password' }) |
21 | | - @ApiResponse({ status: 201, description: 'Password has been successfully changed.'}) |
22 | | - @ApiResponse({ status: 403, description: 'Old password wrong'}) |
23 | | - @ApiResponse({ status: 500, description: 'Backend error'}) |
24 | | - @ApiBearerAuth() |
25 | | - @UseGuards(AuthGuard("api-key")) |
26 | | - async change(@Body() cpwd: ChangePasswordDto,@Res() res: Response): Promise<Response>{ |
27 | | - this.logger.log('call passwd') |
28 | | - const data= await this.passwdService.change(cpwd) |
29 | | - console.log(data) |
30 | | - data.data.uid=cpwd.uid |
31 | | - if ( data.data.status === 0){ |
| 20 | + |
| 21 | + constructor(private passwdService: PasswdService) { |
| 22 | + } |
| 23 | + |
| 24 | + @Post('change') |
| 25 | + @ApiOperation({summary: 'change password'}) |
| 26 | + @ApiResponse({status: 201, description: 'Password has been successfully changed.'}) |
| 27 | + @ApiResponse({status: 403, description: 'Old password wrong'}) |
| 28 | + @ApiResponse({status: 500, description: 'Backend error'}) |
| 29 | + @ApiBearerAuth() |
| 30 | + @UseGuards(AuthGuard("api-key")) |
| 31 | + async change(@Body() cpwd: ChangePasswordDto, @Res() res: Response): Promise<Response> { |
| 32 | + |
| 33 | + const data = await this.passwdService.change(cpwd) |
| 34 | + console.log(data) |
| 35 | + data.data.uid = cpwd.uid |
| 36 | + this.logger.log('call passwd change for : ' + cpwd.uid) |
| 37 | + if (data.data.status === 0) { |
| 38 | + return res.status(201).json(data); |
| 39 | + } else { |
| 40 | + if (data.data.status === 1) { |
| 41 | + return res.status(403).json(data); |
| 42 | + } |
| 43 | + this.logger.log('ERROR : ' + data) |
32 | 44 | return res.status(201).json(data); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + @Post('gettoken') |
| 49 | + @ApiOperation({summary: 'ask token for reseting password'}) |
| 50 | + @ApiResponse({status: 201, description: 'Token', content: {}}) |
| 51 | + @ApiResponse({status: 500, description: 'Backend error'}) |
| 52 | + @ApiBearerAuth() |
| 53 | + @UseGuards(AuthGuard("api-key")) |
| 54 | + async gettoken(@Body() asktoken: AskTokenDto, @Res() res: Response): Promise<Response> { |
| 55 | + const data = await this.passwdService.askToken(asktoken) |
| 56 | + const ret = {token: data} |
| 57 | + console.log(ret) |
| 58 | + return res.status(201).json(ret); |
| 59 | + } |
| 60 | + @Post('verifytoken') |
| 61 | + @ApiOperation({summary: 'ask token for reseting password'}) |
| 62 | + @ApiResponse({status: 201, description: 'Token OK'}) |
| 63 | + @ApiResponse({status: 500, description: 'Token KO'}) |
| 64 | + @ApiBearerAuth() |
| 65 | + @UseGuards(AuthGuard("api-key")) |
| 66 | + async verifyToken(@Body() token:VerifyTokenDto ,@Res() res: Response): Promise<Response> { |
| 67 | + const ok=await this.passwdService.verifyToken(token.token) |
| 68 | + if (ok === true){ |
| 69 | + return res.status(201).json(token) |
| 70 | + }else{ |
| 71 | + return res.status(500).json(token) |
| 72 | + } |
| 73 | + |
| 74 | + } |
| 75 | + @Post('reset') |
| 76 | + @ApiOperation({summary: 'reset password'}) |
| 77 | + @ApiResponse({status: 201, description: 'Reset OK'}) |
| 78 | + @ApiResponse({status: 500, description: 'Reset KO'}) |
| 79 | + @ApiBearerAuth() |
| 80 | + @UseGuards(AuthGuard("api-key")) |
| 81 | + async reset(@Body() data:ResetPasswordDto,@Res()res:Response):Promise<Response>{ |
| 82 | + const tokenData=await this.passwdService.decryptToken(data.token) |
| 83 | + if (Object.keys(tokenData).length === 0){ |
| 84 | + return res.status(500).json({'status':1,'error':'invalid token'}) |
33 | 85 | }else{ |
34 | | - if (data.data.status === 1){ |
35 | | - return res.status(403).json(data); |
36 | | - } |
37 | | - return res.status(201).json(data); |
| 86 | + |
| 87 | + return res.status(200).json({'status':0,'error':''}) |
| 88 | + |
| 89 | + |
38 | 90 | } |
39 | | - } |
| 91 | + |
| 92 | + } |
40 | 93 | } |
| 94 | + |
0 commit comments