feat: add auth support for websocket (#4445)

This commit is contained in:
DarkSky
2023-09-21 21:05:26 +08:00
committed by GitHub
parent 872dc3521b
commit 1ddae40fb2
3 changed files with 86 additions and 9 deletions
+23
View File
@@ -21,6 +21,29 @@ export function getRequestResponseFromContext(context: ExecutionContext) {
res: http.getResponse<Response>(),
};
}
case 'ws': {
const ws = context.switchToWs();
const req = ws.getClient().handshake;
const cookies = req?.headers?.cookie;
// patch cookies to match auth guard logic
if (typeof cookies === 'string') {
req.cookies = cookies
.split(';')
.map(v => v.split('='))
.reduce(
(acc, v) => {
acc[decodeURIComponent(v[0].trim())] = decodeURIComponent(
v[1].trim()
);
return acc;
},
{} as Record<string, string>
);
}
return { req };
}
default:
throw new Error('Unknown context type for getting request and response');
}