Now w/ working cookie based refresh

ref #25
This commit is contained in:
Nicolai Ort 2020-12-12 19:25:40 +01:00
parent 615b54ec4f
commit aca3eaaeea
2 changed files with 11 additions and 5 deletions

View File

@ -1,4 +1,4 @@
import { Body, JsonController, Post, Res } from 'routing-controllers';
import { Body, CookieParam, JsonController, Post, Res } from 'routing-controllers';
import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi';
import { IllegalJWTError, InvalidCredentialsError, JwtNotProvidedError, PasswordNeededError, RefreshTokenCountInvalidError, UsernameOrEmailNeededError } from '../errors/AuthError';
import { UserNotFoundError } from '../errors/UserErrors';
@ -58,13 +58,18 @@ export class AuthController {
@ResponseSchema(UserNotFoundError)
@ResponseSchema(RefreshTokenCountInvalidError)
@OpenAPI({ description: 'refresh a access token' })
async refresh(@Body({ validate: true }) refreshAuth: RefreshAuth) {
async refresh(@CookieParam("lfk_backend__refresh_token") refresh_token: string, @Res() response: any, @Body({ validate: true }) refreshAuth: RefreshAuth) {
if (refresh_token && refresh_token.length != 0) {
refreshAuth.token = refresh_token;
}
let auth;
try {
auth = await refreshAuth.toAuth();
response.cookie('lfk_backend__refresh_token', auth.refresh_token, { expires: new Date(auth.refresh_token_expires_at * 1000), httpOnly: true });
response.cookie('lfk_backend__refresh_token_expires_at', auth.refresh_token_expires_at, { expires: new Date(auth.refresh_token_expires_at * 1000), httpOnly: true });
} catch (error) {
return error;
}
return auth
return response.send(auth)
}
}

View File

@ -1,4 +1,4 @@
import { IsString } from 'class-validator';
import { IsOptional, IsString } from 'class-validator';
import * as jsonwebtoken from 'jsonwebtoken';
import { getConnectionManager } from 'typeorm';
import { config } from '../../config';
@ -8,7 +8,8 @@ import { Auth } from '../responses/ResponseAuth';
export class RefreshAuth {
@IsString()
token: string;
@IsOptional()
token?: string;
public async toAuth(): Promise<Auth> {
let newAuth: Auth = new Auth();