Skip to content

Commit

Permalink
add current error to unauthed routes
Browse files Browse the repository at this point in the history
  • Loading branch information
d-g-town committed May 2, 2024
1 parent dbd715a commit 443900c
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 55 deletions.
3 changes: 2 additions & 1 deletion dashboard/src/main/MainWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import MainWrapperErrorBoundary from "shared/error_handling/MainWrapperErrorBoun
import AuthnProvider from "../shared/auth/AuthnContext";
import { ContextProvider } from "../shared/Context";
import Main from "./Main";
import CurrentError from "./CurrentError";

type PropsType = RouteComponentProps & {};

Expand All @@ -24,4 +25,4 @@ const MainWrapper: React.FC<PropsType> = ({ history, location }) => {
);
};

export default withRouter(MainWrapper);
export default withRouter(MainWrapper);
119 changes: 65 additions & 54 deletions dashboard/src/shared/auth/AuthnContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import ResetPasswordFinalize from "../../main/auth/ResetPasswordFinalize";
import ResetPasswordInit from "../../main/auth/ResetPasswordInit";
import SetInfo from "../../main/auth/SetInfo";
import VerifyEmail from "../../main/auth/VerifyEmail";
import CurrentError from "../../main/CurrentError";

type AuthnState = {
userId: number;
Expand All @@ -32,7 +33,8 @@ const AuthnProvider = ({
}: {
children: JSX.Element;
}): JSX.Element => {
const { setUser, clearContext, setCurrentError } = useContext(Context);
const { setUser, clearContext, setCurrentError, currentError } =
useContext(Context);
const [isLoggedIn, setIsLoggedIn] = useState(false);
const [isEmailVerified, setIsEmailVerified] = useState(false);
const [isLoading, setIsLoading] = useState(true);
Expand Down Expand Up @@ -103,71 +105,80 @@ const AuthnProvider = ({
// return unauthenticated routes
if (!isLoggedIn) {
return (
<Switch>
<Route
path="/login"
render={() => {
return <Login authenticate={authenticate} />;
}}
/>
<Route
path="/register"
render={() => {
return <Register authenticate={authenticate} />;
}}
/>
<Route
path="/password/reset/finalize"
render={() => {
return <ResetPasswordFinalize />;
}}
/>
<Route
path="/password/reset"
render={() => {
return <ResetPasswordInit />;
}}
/>
<Route
path="*"
render={() => {
return <Redirect to="/login" />;
}}
/>
</Switch>
<>
<Switch>
<Route
path="/login"
render={() => {
return <Login authenticate={authenticate} />;
}}
/>
<Route
path="/register"
render={() => {
return <Register authenticate={authenticate} />;
}}
/>
<Route
path="/password/reset/finalize"
render={() => {
return <ResetPasswordFinalize />;
}}
/>
<Route
path="/password/reset"
render={() => {
return <ResetPasswordInit />;
}}
/>
<Route
path="*"
render={() => {
return <Redirect to="/login" />;
}}
/>
</Switch>
<CurrentError currentError={currentError} />
</>
);
}

// if logged in but not verified, block until email verification
if (!local && !isEmailVerified) {
return (
<Switch>
<Route
path="/"
render={() => {
return <VerifyEmail handleLogOut={handleLogOut} />;
}}
/>
</Switch>
<>
<Switch>
<Route
path="/"
render={() => {
return <VerifyEmail handleLogOut={handleLogOut} />;
}}
/>
</Switch>
<CurrentError currentError={currentError} />
</>
);
}

// Handle case where new user signs up via OAuth and has not set name and company
if (!hasInfo && userId > 9312) {
return (
<Switch>
<Route
path="/"
render={() => {
return (
<SetInfo
handleLogOut={handleLogOut}
authenticate={authenticate}
/>
);
}}
/>
</Switch>
<>
<Switch>
<Route
path="/"
render={() => {
return (
<SetInfo
handleLogOut={handleLogOut}
authenticate={authenticate}
/>
);
}}
/>
</Switch>
<CurrentError currentError={currentError} />
</>
);
}

Expand Down

0 comments on commit 443900c

Please sign in to comment.