🚧 starting work on RefreshAuth

ref #12
This commit is contained in:
Philipp Dormann 2020-12-05 12:55:38 +01:00
parent a0fe8c0017
commit 2f902755c4
1 changed files with 36 additions and 0 deletions

View 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;
}
}