Skip to content

AAE-30358 Token is not refreshed in WS connection_init when changing pages #10781

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('WebSocketService', () => {
let service: WebSocketService;
const onLogoutSubject: Subject<void> = new Subject<void>();

const apolloMock = jasmine.createSpyObj('Apollo', ['use', 'createNamed']);
const apolloMock = jasmine.createSpyObj('Apollo', ['use', 'createNamed', 'removeClient']);

beforeEach(() => {
TestBed.configureTestingModule({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ export class WebSocketService {
return this.apollo.use(apolloClientName).subscribe<T>({ errorPolicy: 'all', ...subscriptionOptions });
}

private removeApolloClientIfExists(apolloClientName: string) {
if (this.apollo.use(apolloClientName)) {
this.apollo.removeClient(apolloClientName);
}
}

private get contextRoot() {
return this.appConfigService.get('bpmHost', '');
}
Expand Down Expand Up @@ -135,9 +141,25 @@ export class WebSocketService {
max: Number.POSITIVE_INFINITY,
jitter: true
},
attempts: {
max: 5,
retryIf: (error) => !!error
attempts: (count: number, _operation: Operation, error: any) => {
if (!error) {
return false;
}

const isUnauthorizedError =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can reuse this part to actually catch the error from network when it happens and then try to refresh the token. Same with the retry mechanism instead of having a plain number we can use the retryIf

(Array.isArray(error) &&
error.some(
(err: any) =>
err?.extensions?.code === 'UNAUTHORIZED' ||
err?.message?.includes('4401') ||
err?.message?.toLowerCase().includes('unauthorized')
)) ||
(typeof error === 'string' && (error.includes('4401') || error.toLowerCase().includes('unauthorized'))) ||
(error?.message && (error.message.includes('4401') || error.message.toLowerCase().includes('unauthorized')));

const shouldRetry = isUnauthorizedError ? this.authService.isLoggedIn() : count < 5;

return shouldRetry;
}
});

Expand All @@ -155,10 +177,13 @@ export class WebSocketService {
this.wsLink = new GraphQLWsLink(
createClient({
url: this.createWsUrl(options.wsUrl) + '/v2/ws/graphql',
connectionParams: {
connectionParams: () => ({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense.

Authorization: 'Bearer ' + this.authService.getToken()
},
}),
on: {
closed: () => {
this.removeApolloClientIfExists(options.apolloClientName);
},
error: () => {
this.apollo.removeClient(options.apolloClientName);
this.initSubscriptions(options);
Expand Down
Loading