Skip to content

Commit 700b398

Browse files
committed
refactor(user): update user interceptor
1 parent 41c6443 commit 700b398

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

src/interceptor/user.interceptor.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,30 +23,36 @@ export class UserInterceptor implements NestInterceptor {
2323
{},
2424
context.active(),
2525
(span) => {
26+
const ctxType = executionContext.getType();
27+
let userId: string | null = null;
28+
if (ctxType === 'http') {
29+
const httpReq = executionContext.switchToHttp().getRequest();
30+
if (httpReq.user?.id) {
31+
userId = httpReq.user.id;
32+
}
33+
} else if (ctxType === 'ws') {
34+
const client = executionContext.switchToWs().getClient<Socket>();
35+
userId = client.data.userId;
36+
}
2637
return next.handle().pipe(
2738
tap((responseBody) => {
28-
const ctxType = executionContext.getType();
29-
let userId: string | null = null;
30-
if (ctxType === 'http') {
39+
if (!userId && ctxType === 'http') {
3140
const httpReq = executionContext.switchToHttp().getRequest();
32-
if (httpReq.user?.id) {
33-
userId = httpReq.user.id;
34-
} else if (
41+
if (
3542
LOGIN_URLS.includes(httpReq.url) &&
3643
httpReq.method === 'POST' &&
3744
responseBody?.id
3845
) {
3946
userId = responseBody.id;
4047
}
41-
} else if (ctxType === 'ws') {
42-
const client = executionContext.switchToWs().getClient<Socket>();
43-
userId = client.data.userId;
4448
}
49+
}),
50+
finalize(() => {
4551
if (userId) {
4652
span.setAttribute('user.id', userId);
4753
}
54+
span.end();
4855
}),
49-
finalize(() => span.end()),
5056
);
5157
},
5258
);

0 commit comments

Comments
 (0)