Skip to content

Commit

Permalink
Merge pull request #155 from jadmsaadaot/SUBMIT-task#133
Browse files Browse the repository at this point in the history
Handle different errors when getUser fails on login
  • Loading branch information
djnunez-aot authored Nov 13, 2024
2 parents 26a49f6 + 7a23453 commit d0a1549
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 31 deletions.
27 changes: 27 additions & 0 deletions submit-web/src/components/ErrorPage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Paper, Container } from "@mui/material";
import { useEffect } from "react";
import { useAuth } from "react-oidc-context";

export default function ErrorPage() {
const { signoutRedirect } = useAuth();

useEffect(() => {
signoutRedirect();
}, [signoutRedirect]);

return (
<Container id="Error">
<Paper
elevation={3}
sx={{
padding: "1rem",
marginTop: "2rem",
textAlign: "center",
}}
>
<p>Oops! something wrong happened.</p>
<p>You will be signed out.</p>
</Paper>
</Container>
);
}
3 changes: 2 additions & 1 deletion submit-web/src/routes/error.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import ErrorPage from "@/components/ErrorPage";
import { createFileRoute } from "@tanstack/react-router";

export const Route = createFileRoute("/error")({
Expand All @@ -6,5 +7,5 @@ export const Route = createFileRoute("/error")({
});

function Error() {
return <div>Oops! An unexpected error occurred</div>;
return <ErrorPage />;
}
65 changes: 35 additions & 30 deletions submit-web/src/routes/oidc-callback/index.tsx
Original file line number Diff line number Diff line change
@@ -1,61 +1,66 @@
import { PageLoader } from "@/components/Shared/PageLoader";
import { notify } from "@/components/Shared/Snackbar/snackbarStore";
import { useGetUserByGuid } from "@/hooks/api/useAccounts";
import { USER_TYPE } from "@/models/User";
import { HTTP_STATUS } from "@/utils/constants";
import { createFileRoute, Navigate } from "@tanstack/react-router";
import { useEffect, useState } from "react";
import { isAxiosError } from "axios";
import { useAuth } from "react-oidc-context";

export const Route = createFileRoute("/oidc-callback/")({
component: OidcCallback,
// loader: ({ context: { authentication, queryClient } }) => {
// return queryClient.ensureQueryData(
// getUserByGuidQueryOptions({ guid: authentication?.user?.profile.sub }),
// );
// },
// errorComponent: () => <Navigate to="/error" />,
// pendingComponent: () => <PageLoader />,
});

const ERROR_MESSAGE = "An error occurred while loading user data";

function OidcCallback() {
const { error: getAuthError, user: kcUser } = useAuth();
const [isAuthLoading, setIsAuthLoading] = useState(true);
const params = new URLSearchParams(window.location.search);
const proponent_id = params.get("proponent_id");

useEffect(() => {
if (kcUser) {
setIsAuthLoading(false);
}
}, [kcUser, setIsAuthLoading]);

const { data: userData, isLoading: isUserDataLoading } = useGetUserByGuid({
const { data: userData, error: getUserError } = useGetUserByGuid({
guid: kcUser?.profile.sub,
});

if (getAuthError) {
return <Navigate to="/error" />;
}

if (getUserError) {
if (isAxiosError(getUserError)) {
if (
getUserError.response?.status === HTTP_STATUS.NOT_FOUND &&
proponent_id
) {
return (
<Navigate
to="/proponent/registration/create-account"
search={{
proponent_id: proponent_id
? Number.parseInt(proponent_id)
: undefined,
}}
/>
);
} else {
notify.error(getUserError.response?.data?.message || ERROR_MESSAGE);
return <Navigate to="/error" />;
}
}
notify.error(ERROR_MESSAGE);
return <Navigate to="/error" />;
}

if (userData?.type === USER_TYPE.STAFF) {
return <Navigate to="/staff/projects" />;
}

if (userData?.account_user?.account_id) {
if (
userData?.type === USER_TYPE.PROPONENT &&
userData?.account_user?.account_id
) {
return <Navigate to="/proponent/projects" />;
}

if (!isAuthLoading && !isUserDataLoading) {
return (
<Navigate
to="/proponent/registration/create-account"
search={{
proponent_id: proponent_id
? Number.parseInt(proponent_id)
: undefined,
}}
/>
);
}

return <PageLoader />;
}
10 changes: 10 additions & 0 deletions submit-web/src/utils/constants.ts
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
// App Constants should go here
export const HTTP_STATUS = {
OK: 200,
CREATED: 201,
NO_CONTENT: 204,
BAD_REQUEST: 400,
UNAUTHORIZED: 401,
FORBIDDEN: 403,
NOT_FOUND: 404,
INTERNAL_SERVER_ERROR: 500,
};

0 comments on commit d0a1549

Please sign in to comment.