46
									
								
								src/models/actions/ResetPassword.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										46
									
								
								src/models/actions/ResetPassword.ts
									
									
									
									
									
										Normal file
									
								
							@@ -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<any> {
 | 
			
		||||
        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;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user