HBE-164 refactor: subscriptions auth cookie fix (#26)

* chore: added error handling to cookie extraction logic for subscriptions

* chore: removed migration file from prisma directory
This commit is contained in:
Balu Babu
2023-02-27 19:32:33 +05:30
committed by GitHub
parent 292ed87201
commit 2ba05a46ee
5 changed files with 6421 additions and 19 deletions

View File

@@ -1,8 +1,10 @@
import { HttpException, HttpStatus } from '@nestjs/common';
import { ForbiddenException, HttpException, HttpStatus } from '@nestjs/common';
import { DateTime } from 'luxon';
import { AuthError } from 'src/types/AuthError';
import { AuthTokens } from 'src/types/AuthTokens';
import { Response } from 'express';
import * as cookie from 'cookie';
import { COOKIES_NOT_FOUND } from 'src/errors';
enum AuthTokenType {
ACCESS_TOKEN = 'access_token',
@@ -64,14 +66,19 @@ export const authCookieHandler = (
* @returns AuthTokens for JWT strategy to use
*/
export const subscriptionContextCookieParser = (rawCookies: string) => {
const cookieMap = new Map<string, string>();
rawCookies.split(';').forEach((cookie) => {
const [key, value] = cookie.split('=');
cookieMap.set(key, value);
});
const cookies = cookie.parse(rawCookies);
if (
!cookies[AuthTokenType.ACCESS_TOKEN] &&
!cookies[AuthTokenType.REFRESH_TOKEN]
) {
throw new HttpException(COOKIES_NOT_FOUND, 400, {
cause: new Error(COOKIES_NOT_FOUND),
});
}
return <AuthTokens>{
access_token: cookieMap.get(AuthTokenType.ACCESS_TOKEN),
refresh_token: cookieMap.get(AuthTokenType.REFRESH_TOKEN),
access_token: cookies[AuthTokenType.ACCESS_TOKEN],
refresh_token: cookies[AuthTokenType.REFRESH_TOKEN],
};
};