apps/api: add /reset-password method
This commit is contained in:
parent
76c1e0f8d1
commit
01f4378e11
4
.vscode/settings.json
vendored
4
.vscode/settings.json
vendored
@ -14,8 +14,8 @@
|
|||||||
"editor.formatOnSave": true,
|
"editor.formatOnSave": true,
|
||||||
"editor.codeActionsOnSave": {
|
"editor.codeActionsOnSave": {
|
||||||
"source.fixAll": true,
|
"source.fixAll": true,
|
||||||
"source.fixAll.eslint": true,
|
"source.fixAll.eslint": true
|
||||||
"source.removeUnusedImports": true
|
// "source.removeUnusedImports": true
|
||||||
},
|
},
|
||||||
"workbench.editor.labelFormat": "short",
|
"workbench.editor.labelFormat": "short",
|
||||||
"eslint.workingDirectories": [
|
"eslint.workingDirectories": [
|
||||||
|
|||||||
@ -3,6 +3,7 @@
|
|||||||
/* eslint-disable import/no-extraneous-dependencies */
|
/* eslint-disable import/no-extraneous-dependencies */
|
||||||
import { AccountService } from './account.service';
|
import { AccountService } from './account.service';
|
||||||
import { CreateAccountDto } from './dto/create-account.dto';
|
import { CreateAccountDto } from './dto/create-account.dto';
|
||||||
|
import { ResetPasswordDto } from './dto/reset-password.dto';
|
||||||
import {
|
import {
|
||||||
Body,
|
Body,
|
||||||
Controller,
|
Controller,
|
||||||
@ -84,6 +85,21 @@ export class AccountController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Post('/reset-password')
|
||||||
|
@ApiResponse({
|
||||||
|
status: HttpStatus.OK,
|
||||||
|
type: Account,
|
||||||
|
})
|
||||||
|
async resetPassword(@Body() resetPasswordDto: ResetPasswordDto, @Res() reply: FastifyReply) {
|
||||||
|
try {
|
||||||
|
const updatedAccount = await this.accountService.resetPassword(resetPasswordDto);
|
||||||
|
|
||||||
|
return reply.status(HttpStatus.OK).send(updatedAccount);
|
||||||
|
} catch (error) {
|
||||||
|
throw new HttpException(error, HttpStatus.BAD_REQUEST);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Post('/login')
|
@Post('/login')
|
||||||
async login(@Body() credentials: Credentials, @Res() reply: FastifyReply) {
|
async login(@Body() credentials: Credentials, @Res() reply: FastifyReply) {
|
||||||
try {
|
try {
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
import type { CreateAccountDto } from './dto/create-account.dto';
|
import type { CreateAccountDto } from './dto/create-account.dto';
|
||||||
|
import type { ResetPasswordDto } from './dto/reset-password.dto';
|
||||||
import { BadRequestException, Injectable, UnauthorizedException } from '@nestjs/common';
|
import { BadRequestException, Injectable, UnauthorizedException } from '@nestjs/common';
|
||||||
import { JwtService } from '@nestjs/jwt';
|
import { JwtService } from '@nestjs/jwt';
|
||||||
import { InjectModel } from '@nestjs/mongoose';
|
import { InjectModel } from '@nestjs/mongoose';
|
||||||
@ -53,11 +54,20 @@ export class AccountService {
|
|||||||
throw new BadRequestException(`Prop ${field} is not allowed`);
|
throw new BadRequestException(`Prop ${field} is not allowed`);
|
||||||
});
|
});
|
||||||
|
|
||||||
this.accountModel.findOneAndUpdate({ username }, props).exec();
|
await this.accountModel.findOneAndUpdate({ username }, props).exec();
|
||||||
|
|
||||||
return this.accountModel.findOne({ username });
|
return this.accountModel.findOne({ username });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async resetPassword({ username }: ResetPasswordDto): Promise<Account> {
|
||||||
|
const account = await this.accountModel.findOne({ username });
|
||||||
|
if (!account) throw new UnauthorizedException('Account not found');
|
||||||
|
|
||||||
|
const new_password = generatePassword();
|
||||||
|
|
||||||
|
return { password: new_password, username };
|
||||||
|
}
|
||||||
|
|
||||||
public async login({ login, password }: Credentials) {
|
public async login({ login, password }: Credentials) {
|
||||||
try {
|
try {
|
||||||
const account = await this.accountModel.findOne({ username: login });
|
const account = await this.accountModel.findOne({ username: login });
|
||||||
|
|||||||
9
apps/api/src/account/dto/reset-password.dto.ts
Normal file
9
apps/api/src/account/dto/reset-password.dto.ts
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
import { ApiProperty } from '@nestjs/swagger';
|
||||||
|
import { IsNotEmpty, IsString } from 'class-validator';
|
||||||
|
|
||||||
|
export class ResetPasswordDto {
|
||||||
|
@ApiProperty()
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public readonly username: string;
|
||||||
|
}
|
||||||
@ -35,3 +35,17 @@ AccountSchema.pre('save', async function (next) {
|
|||||||
return next(error);
|
return next(error);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
AccountSchema.pre('findOneAndUpdate', async function (next) {
|
||||||
|
try {
|
||||||
|
const password = this.get('password');
|
||||||
|
if (password) {
|
||||||
|
const hash = await bcrypt.hash(password, 10);
|
||||||
|
this.set('password', hash);
|
||||||
|
}
|
||||||
|
|
||||||
|
return next();
|
||||||
|
} catch (error) {
|
||||||
|
return next(error);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user