refactor: changed onConnect function in subscriptionHandler to decode cookies for subscriptions
This commit is contained in:
@@ -7,6 +7,7 @@ import { AuthModule } from './auth/auth.module';
|
||||
import { UserSettingsModule } from './user-settings/user-settings.module';
|
||||
import { UserEnvironmentsModule } from './user-environment/user-environments.module';
|
||||
import { UserHistoryModule } from './user-history/user-history.module';
|
||||
import { subscriptionContextCookieParser } from './auth/helper';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
@@ -22,14 +23,12 @@ import { UserHistoryModule } from './user-history/user-history.module';
|
||||
subscriptions: {
|
||||
'subscriptions-transport-ws': {
|
||||
path: '/graphql',
|
||||
onConnect: (connectionParams: any) => {
|
||||
onConnect: (_, websocket) => {
|
||||
const cookies = subscriptionContextCookieParser(
|
||||
websocket.upgradeReq.headers.cookie,
|
||||
);
|
||||
return {
|
||||
reqHeaders: Object.fromEntries(
|
||||
Object.entries(connectionParams).map(([k, v]) => [
|
||||
k.toLowerCase(),
|
||||
v,
|
||||
]),
|
||||
),
|
||||
headers: { ...websocket?.upgradeReq?.headers, cookies },
|
||||
};
|
||||
},
|
||||
},
|
||||
|
||||
@@ -52,3 +52,17 @@ export const authCookieHandler = (
|
||||
res.status(HttpStatus.OK).redirect(process.env.REDIRECT_URL);
|
||||
} else res.status(HttpStatus.OK).send();
|
||||
};
|
||||
|
||||
/**
|
||||
* Sets and returns the cookies in the response object on successful authentication
|
||||
* @param rawCookies cookies from the websocket connection
|
||||
* @returns AuthTokens for JWT strategy to use
|
||||
*/
|
||||
export const subscriptionContextCookieParser = (rawCookies: string) => {
|
||||
const access_tokenString = rawCookies.split(';')[0].split('=')[1];
|
||||
const refresh_tokenString = rawCookies.split(';')[1].split('=')[1];
|
||||
return <AuthTokens>{
|
||||
access_token: access_tokenString,
|
||||
refresh_token: refresh_tokenString,
|
||||
};
|
||||
};
|
||||
|
||||
@@ -37,8 +37,6 @@ export class JwtStrategy extends PassportStrategy(Strategy, 'jwt') {
|
||||
if (!payload) throw new ForbiddenException(INVALID_ACCESS_TOKEN);
|
||||
|
||||
const user = await this.usersService.findUserById(payload.sub);
|
||||
console.log('user', user);
|
||||
|
||||
if (O.isNone(user)) {
|
||||
throw new UnauthorizedException(USER_NOT_FOUND);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user