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 { UserSettingsModule } from './user-settings/user-settings.module';
|
||||||
import { UserEnvironmentsModule } from './user-environment/user-environments.module';
|
import { UserEnvironmentsModule } from './user-environment/user-environments.module';
|
||||||
import { UserHistoryModule } from './user-history/user-history.module';
|
import { UserHistoryModule } from './user-history/user-history.module';
|
||||||
|
import { subscriptionContextCookieParser } from './auth/helper';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [
|
imports: [
|
||||||
@@ -22,14 +23,12 @@ import { UserHistoryModule } from './user-history/user-history.module';
|
|||||||
subscriptions: {
|
subscriptions: {
|
||||||
'subscriptions-transport-ws': {
|
'subscriptions-transport-ws': {
|
||||||
path: '/graphql',
|
path: '/graphql',
|
||||||
onConnect: (connectionParams: any) => {
|
onConnect: (_, websocket) => {
|
||||||
|
const cookies = subscriptionContextCookieParser(
|
||||||
|
websocket.upgradeReq.headers.cookie,
|
||||||
|
);
|
||||||
return {
|
return {
|
||||||
reqHeaders: Object.fromEntries(
|
headers: { ...websocket?.upgradeReq?.headers, cookies },
|
||||||
Object.entries(connectionParams).map(([k, v]) => [
|
|
||||||
k.toLowerCase(),
|
|
||||||
v,
|
|
||||||
]),
|
|
||||||
),
|
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -52,3 +52,17 @@ export const authCookieHandler = (
|
|||||||
res.status(HttpStatus.OK).redirect(process.env.REDIRECT_URL);
|
res.status(HttpStatus.OK).redirect(process.env.REDIRECT_URL);
|
||||||
} else res.status(HttpStatus.OK).send();
|
} 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);
|
if (!payload) throw new ForbiddenException(INVALID_ACCESS_TOKEN);
|
||||||
|
|
||||||
const user = await this.usersService.findUserById(payload.sub);
|
const user = await this.usersService.findUserById(payload.sub);
|
||||||
console.log('user', user);
|
|
||||||
|
|
||||||
if (O.isNone(user)) {
|
if (O.isNone(user)) {
|
||||||
throw new UnauthorizedException(USER_NOT_FOUND);
|
throw new UnauthorizedException(USER_NOT_FOUND);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user