Renamed the password reset token creation class to better fit the scheme

ref #40
This commit is contained in:
2020-12-22 10:39:42 +01:00
parent 61aff5e629
commit aef8485f59

View File

@@ -0,0 +1,47 @@
import { IsEmail, IsOptional, IsString } from 'class-validator';
import { getConnectionManager } from 'typeorm';
import { UserNotFoundError } from '../../errors/AuthError';
import { UsernameOrEmailNeededError } from '../../errors/UserErrors';
import { JwtCreator } from '../../jwtcreator';
import { User } from '../entities/User';
/**
* TODO:
*/
export class CreateResetToken {
/**
* The username of the user that wants to reset their password.
*/
@IsOptional()
@IsString()
username?: string;
/**
* The email address of the user that wants to reset their password.
*/
@IsOptional()
@IsEmail()
@IsString()
email?: string;
/**
* Create a password reset token based on this.
*/
public async toResetToken(): Promise<any> {
if (this.email === undefined && this.username === undefined) {
throw new UsernameOrEmailNeededError();
}
let found_user = await getConnectionManager().get().getRepository(User).findOne({ relations: ['groups', 'permissions', 'actions'], where: [{ username: this.username }, { email: this.email }] });
if (!found_user) {
throw new UserNotFoundError();
}
found_user.refreshTokenCount = found_user.refreshTokenCount + 1;
await getConnectionManager().get().getRepository(User).save(found_user);
//Create the reset
let access_token = JwtCreator.createReset(found_user);
return access_token;
}
}