Skip to content

1077 Logout faster when session expires and when token doesnt work in api calls #1103

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

Merged
merged 4 commits into from
May 14, 2024
Merged
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
5 changes: 5 additions & 0 deletions .changeset/spicy-olives-itch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@orchestrator-ui/orchestrator-ui-components": minor
---

Signs user out when api calls fail because of expired token
2 changes: 1 addition & 1 deletion apps/wfo-ui
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import React, { useEffect } from 'react';

import { getSession, signOut } from 'next-auth/react';
import { useRouter } from 'next/router';

import { useGetOrchestratorConfig } from '@/hooks';

export const WfoRouteChangeListener = () => {
const router = useRouter();
const { authActive } = useGetOrchestratorConfig();

useEffect(() => {
if (authActive) {
getSession().then((session) => {
if (!session) {
signOut();
}
});
}
}, [authActive, router]);
return <></>;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './WfoRouteChangeListener';
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ export * from './WfoNoResults';
export * from './WfoStartButton';
export * from './WfoSubscriptionsList';
export * from './WfoSummary';
export * from './WfoRouteChangeListener';
17 changes: 16 additions & 1 deletion packages/orchestrator-ui-components/src/rtk/api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getSession } from 'next-auth/react';
import { getSession, signOut } from 'next-auth/react';

import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react';
import { graphqlRequestBaseQuery } from '@rtk-query/graphql-request-base-query';
Expand Down Expand Up @@ -85,6 +85,21 @@ export const orchestratorApi = createApi({
const graphqlFn = graphqlRequestBaseQuery({
url: customApi ? customApi.apiBaseUrl : graphqlEndpointCore,
prepareHeaders,
customErrors: (error) => {
const { name, message, stack, response } = error;
if (response?.errors && response.errors?.length > 0) {
response.errors.map((error) => {
// TODO: https://github.com/workfloworchestrator/orchestrator-ui-library/issues/1105
if (
error.extensions?.error_type ===
'not_authorized'
) {
signOut();
}
});
}
return { name, message, stack };
},
});
return graphqlFn(args, api, {});
}
Expand Down
Loading