Skip to content

Commit a2ad2b7

Browse files
fix: no automatic logout and redirect when token invalid ml-174 (#335)
* feat(frontend): add invalid token check ml-174 * refactor(frontend): fix protected route token check ml-174 * feat(frontend): add rejected payload type ml-174 * feat(frontend): add action status check ml-174 * refactor(frontend): fix invalid token check ml-174 * feat(frontend): add redirect variable ml-174 * refactor(frontend): fix error handling ml-174 * refactor(frontend): fix invalid token action notification ml-174 * refactor(frontend): fix invalid token notification while navigate ml-174 * refactor(frontend): fix get meeting details actions ml-174
1 parent cdc9760 commit a2ad2b7

File tree

4 files changed

+40
-9
lines changed

4 files changed

+40
-9
lines changed

apps/frontend/src/libs/components/router-provider/protected-route.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ const ProtectedRoute: React.FC<Properties> = ({
2626

2727
if (shouldRedirect) {
2828
return (
29-
<Navigate replace state={{ from: location.pathname }} to={redirectTo} />
29+
<Navigate
30+
replace
31+
state={{ from: location.pathname, showNotification: true }}
32+
to={redirectTo}
33+
/>
3034
);
3135
}
3236

apps/frontend/src/libs/modules/middlewares/error-listener.middleware.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ errorListenerMiddleware.startListening({
1818
const payload = action.payload as RejectPayload | undefined;
1919

2020
if (payload?.status === HTTPCode.UNAUTHORIZED) {
21-
notification.error(
22-
payload.message ?? "Session expired. Please log in again.",
23-
);
2421
await listenerApi.dispatch(authActions.logout());
22+
notification.error("Session expired. Please log in again.");
23+
24+
return;
2525
}
2626

2727
const errorMessage =

apps/frontend/src/modules/meeting-details/slices/actions.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { createAsyncThunk } from "@reduxjs/toolkit";
22

3+
import { HTTPError } from "~/libs/modules/http/http.js";
34
import { type AsyncThunkConfig } from "~/libs/types/types.js";
45
import {
56
type MeetingDetailedResponseDto,
@@ -14,12 +15,23 @@ const getMeetingDetailsById = createAsyncThunk<
1415
AsyncThunkConfig
1516
>(
1617
`${sliceName}/get-meeting-details-by-id`,
17-
async ({ id, sharedToken }, { extra }) => {
18-
const { meetingDetailsApi } = extra;
18+
async ({ id, sharedToken }, { extra, rejectWithValue }) => {
19+
try {
20+
const { meetingDetailsApi } = extra;
1921

20-
return await (sharedToken
21-
? meetingDetailsApi.getMeetingByIdPublic(id, sharedToken)
22-
: meetingDetailsApi.getMeetingByIdAuth(id));
22+
return await (sharedToken
23+
? meetingDetailsApi.getMeetingByIdPublic(id, sharedToken)
24+
: meetingDetailsApi.getMeetingByIdAuth(id));
25+
} catch (error) {
26+
if (error instanceof HTTPError) {
27+
return rejectWithValue({
28+
message: error.message,
29+
status: error.status,
30+
});
31+
}
32+
33+
return rejectWithValue({ message: "Something went wrong" });
34+
}
2335
},
2436
);
2537

apps/frontend/src/pages/auth/auth.tsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import {
44
useAppDispatch,
55
useAppSelector,
66
useCallback,
7+
useEffect,
78
useLocation,
89
} from "~/libs/hooks/hooks.js";
10+
import { notification } from "~/libs/modules/notifications/notifications.js";
911
import { actions as authActions } from "~/modules/auth/auth.js";
1012
import {
1113
type UserSignInRequestDto,
@@ -14,11 +16,24 @@ import {
1416

1517
import { SignInForm, SignUpForm } from "./components/components.js";
1618

19+
type LocationState = null | {
20+
from?: string;
21+
showNotification?: boolean;
22+
};
23+
1724
const Auth: React.FC = () => {
1825
const dispatch = useAppDispatch();
1926
const { dataStatus, user } = useAppSelector((state) => state.auth);
2027
const { pathname } = useLocation();
2128
const hasUser = Boolean(user);
29+
const location = useLocation() as { state: LocationState };
30+
31+
useEffect(() => {
32+
if (location.state?.showNotification) {
33+
notification.error("Session expired. Please log in again.");
34+
location.state.showNotification = false;
35+
}
36+
}, [location.state]);
2237

2338
const handleSignInSubmit = useCallback(
2439
(payload: UserSignInRequestDto): void => {

0 commit comments

Comments
 (0)