HBE 145 - fixes cookie parse issue (#17)

* feat: handled cookie parsing

* chore: enum added

* chore: enum name updated
This commit is contained in:
Mir Arif Hasan
2023-02-10 12:39:04 +06:00
committed by GitHub
parent 7336a3d9c7
commit b60d45ba76
2 changed files with 15 additions and 7 deletions

View File

@@ -4,6 +4,11 @@ import { AuthError } from 'src/types/AuthError';
import { AuthTokens } from 'src/types/AuthTokens'; import { AuthTokens } from 'src/types/AuthTokens';
import { Response } from 'express'; import { Response } from 'express';
enum AuthTokenType {
ACCESS_TOKEN = 'access_token',
REFRESH_TOKEN = 'refresh_token',
}
/** /**
* This function allows throw to be used as an expression * This function allows throw to be used as an expression
* @param errMessage Message present in the error message * @param errMessage Message present in the error message
@@ -36,13 +41,13 @@ export const authCookieHandler = (
}) })
.toMillis(); .toMillis();
res.cookie('access_token', authTokens.access_token, { res.cookie(AuthTokenType.ACCESS_TOKEN, authTokens.access_token, {
httpOnly: true, httpOnly: true,
secure: true, secure: true,
sameSite: 'lax', sameSite: 'lax',
maxAge: accessTokenValidity, maxAge: accessTokenValidity,
}); });
res.cookie('refresh_token', authTokens.refresh_token, { res.cookie(AuthTokenType.REFRESH_TOKEN, authTokens.refresh_token, {
httpOnly: true, httpOnly: true,
secure: true, secure: true,
sameSite: 'lax', sameSite: 'lax',
@@ -59,10 +64,14 @@ export const authCookieHandler = (
* @returns AuthTokens for JWT strategy to use * @returns AuthTokens for JWT strategy to use
*/ */
export const subscriptionContextCookieParser = (rawCookies: string) => { export const subscriptionContextCookieParser = (rawCookies: string) => {
const access_token = rawCookies.split(';')[0].split('=')[1]; const cookieMap = new Map<string, string>();
const refresh_token = rawCookies.split(';')[1].split('=')[1]; rawCookies.split(';').forEach((cookie) => {
const [key, value] = cookie.split('=');
cookieMap.set(key, value);
});
return <AuthTokens>{ return <AuthTokens>{
access_token, access_token: cookieMap.get(AuthTokenType.ACCESS_TOKEN),
refresh_token, refresh_token: cookieMap.get(AuthTokenType.REFRESH_TOKEN),
}; };
}; };

View File

@@ -8,7 +8,6 @@ import * as T from 'fp-ts/Task';
import * as E from 'fp-ts/Either'; import * as E from 'fp-ts/Either';
import * as A from 'fp-ts/Array'; import * as A from 'fp-ts/Array';
import { TeamMemberRole } from './team/team.model'; import { TeamMemberRole } from './team/team.model';
import { User } from './user/user.model';
import { JSON_INVALID } from './errors'; import { JSON_INVALID } from './errors';
/** /**