36
									
								
								src/models/creation/RefreshAuth.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										36
									
								
								src/models/creation/RefreshAuth.ts
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,36 @@
 | 
			
		||||
import { IsString } from 'class-validator';
 | 
			
		||||
import * as jsonwebtoken from 'jsonwebtoken';
 | 
			
		||||
import { getConnectionManager } from 'typeorm';
 | 
			
		||||
import { IllegalJWTError, JwtNotProvidedError, UserNotFoundError } from '../../errors/AuthError';
 | 
			
		||||
import { Auth } from '../entities/Auth';
 | 
			
		||||
import { User } from '../entities/User';
 | 
			
		||||
 | 
			
		||||
export class RefreshAuth {
 | 
			
		||||
    @IsString()
 | 
			
		||||
    token: string;
 | 
			
		||||
 | 
			
		||||
    public async toAuth(): Promise<Auth> {
 | 
			
		||||
        let newAuth: Auth = new Auth();
 | 
			
		||||
        if (!this.token || this.token === undefined) {
 | 
			
		||||
            throw new JwtNotProvidedError()
 | 
			
		||||
        }
 | 
			
		||||
        let decoded
 | 
			
		||||
        try {
 | 
			
		||||
            decoded = jsonwebtoken.verify(this.token, 'securekey')
 | 
			
		||||
        } catch (error) {
 | 
			
		||||
            throw new IllegalJWTError()
 | 
			
		||||
        }
 | 
			
		||||
        const found_users = await getConnectionManager().get().getRepository(User).findOne({ id: decoded["userid"], refreshTokenCount: decoded["refreshtokencount"] });
 | 
			
		||||
        if (!found_users) {
 | 
			
		||||
            throw new UserNotFoundError()
 | 
			
		||||
        } else {
 | 
			
		||||
            const found_user = found_users[0]
 | 
			
		||||
            delete found_user.password;
 | 
			
		||||
            newAuth.access_token = "ja"
 | 
			
		||||
            newAuth.access_token_expires_at = 5555555
 | 
			
		||||
            newAuth.refresh_token = "ja"
 | 
			
		||||
            newAuth.refresh_token_expires_at = 555555
 | 
			
		||||
        }
 | 
			
		||||
        return newAuth;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user