parent
0d21497c2f
commit
675717f8ca
|
@ -3,6 +3,7 @@ import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi';
|
|||
import { IllegalJWTError, InvalidCredentialsError, JwtNotProvidedError, PasswordNeededError, RefreshTokenCountInvalidError, UsernameOrEmailNeededError } from '../errors/AuthError';
|
||||
import { UserNotFoundError } from '../errors/UserErrors';
|
||||
import { CreateAuth } from '../models/creation/CreateAuth';
|
||||
import { HandleLogout } from '../models/creation/HandleLogout';
|
||||
import { RefreshAuth } from '../models/creation/RefreshAuth';
|
||||
|
||||
@JsonController('/auth')
|
||||
|
@ -28,6 +29,24 @@ export class AuthController {
|
|||
return auth
|
||||
}
|
||||
|
||||
@Post("/logout")
|
||||
@ResponseSchema(InvalidCredentialsError)
|
||||
@ResponseSchema(UserNotFoundError)
|
||||
@ResponseSchema(UsernameOrEmailNeededError)
|
||||
@ResponseSchema(PasswordNeededError)
|
||||
@ResponseSchema(InvalidCredentialsError)
|
||||
@OpenAPI({ description: 'Create a new access token object' })
|
||||
async logout(@Body({ validate: true }) handleLogout: HandleLogout) {
|
||||
let logout;
|
||||
try {
|
||||
logout = await handleLogout.logout()
|
||||
console.log(logout);
|
||||
} catch (error) {
|
||||
return error;
|
||||
}
|
||||
return logout
|
||||
}
|
||||
|
||||
@Post("/refresh")
|
||||
@ResponseSchema(JwtNotProvidedError)
|
||||
@ResponseSchema(IllegalJWTError)
|
||||
|
|
|
@ -0,0 +1,33 @@
|
|||
import { IsString } from 'class-validator';
|
||||
import * as jsonwebtoken from 'jsonwebtoken';
|
||||
import { IllegalJWTError, JwtNotProvidedError } from '../../errors/AuthError';
|
||||
import { Logout } from '../entities/Logout';
|
||||
|
||||
export class HandleLogout {
|
||||
@IsString()
|
||||
token: string;
|
||||
|
||||
public async logout(): Promise<Logout> {
|
||||
let logout: Logout = new Logout();
|
||||
if (!this.token || this.token === undefined) {
|
||||
throw new JwtNotProvidedError()
|
||||
}
|
||||
let decoded
|
||||
try {
|
||||
decoded = jsonwebtoken.verify(this.token, 'securekey')
|
||||
} catch (error) {
|
||||
throw new IllegalJWTError()
|
||||
}
|
||||
logout.access_token = this.token
|
||||
logout.timestamp = Math.floor(Date.now() / 1000)
|
||||
// const found_user = await getConnectionManager().get().getRepository(User).findOne({ id: decoded["userid"] });
|
||||
// if (!found_user) {
|
||||
// throw new UserNotFoundError()
|
||||
// }
|
||||
// if (found_user.refreshTokenCount !== decoded["refreshtokencount"]) {
|
||||
// throw new RefreshTokenCountInvalidError()
|
||||
// }
|
||||
// TODO: increment refreshtokencount WHERE userid===userid && refreshtokencount===refreshtokencount
|
||||
return logout;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
import { IsString } from 'class-validator';
|
||||
|
||||
/**
|
||||
* Defines a Logout object
|
||||
*/
|
||||
export class Logout {
|
||||
/**
|
||||
* access_token - JWT shortterm access token
|
||||
*/
|
||||
@IsString()
|
||||
access_token: string;
|
||||
/**
|
||||
* timestamp of logout
|
||||
*/
|
||||
@IsString()
|
||||
timestamp: number;
|
||||
}
|
Loading…
Reference in New Issue