Skip to content

fix: redirect to login upon authentication error #9134

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 12 commits into from
Aug 17, 2023
Next Next commit
very wrong, but it technically works :^)
  • Loading branch information
aslilac committed Aug 16, 2023
commit 61c25caab4cad14d55a3f0f85d412eff8f6da56e
3 changes: 3 additions & 0 deletions site/src/api/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,6 @@ export const getErrorDetail = (error: unknown): string | undefined | null =>
: error instanceof Error
? `Please check the developer console for more details.`
: null

export const isAuthenticationError = (error: unknown): boolean =>
isApiError(error) && error.response.status === 401
10 changes: 9 additions & 1 deletion site/src/components/Alert/ErrorAlert.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
import { AlertProps, Alert, AlertDetail } from "./Alert"
import AlertTitle from "@mui/material/AlertTitle"
import { getErrorMessage, getErrorDetail } from "api/errors"
import {
getErrorMessage,
getErrorDetail,
isAuthenticationError,
} from "api/errors"
import { FC } from "react"

export const ErrorAlert: FC<
Omit<AlertProps, "severity" | "children"> & { error: unknown }
> = ({ error, ...alertProps }) => {
if (isAuthenticationError(error)) {
location.href = "/login"
}

const message = getErrorMessage(error, "Something went wrong.")
const detail = getErrorDetail(error)

Expand Down