diff --git a/src/models/actions/ResetPassword.ts b/src/models/actions/ResetPassword.ts new file mode 100644 index 0000000..186f4cb --- /dev/null +++ b/src/models/actions/ResetPassword.ts @@ -0,0 +1,46 @@ +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 ResetPassword { + /** + * The username of the user that want's to login. + * Either username or email have to be provided. + */ + @IsOptional() + @IsString() + username?: string; + + /** + * The email address of the user that want's to login. + * Either username or email have to be provided. + */ + @IsOptional() + @IsEmail() + @IsString() + email?: string; + + + /** + * Reset a password based on this. + */ + public async toResetToken(): Promise { + if (this.email === undefined && this.username === undefined) { + throw new UsernameOrEmailNeededError(); + } + const found_user = await getConnectionManager().get().getRepository(User).findOne({ where: [{ username: this.username }, { email: this.email }] }); + if (!found_user) { + throw new UserNotFoundError(); + } + + //Create the reset + let access_token = JwtCreator.createReset(found_user); + return access_token; + } +} \ No newline at end of file