Skip to content

Commit 4e42702

Browse files
committed
actually this way worked a little bit better
1 parent 3d4e398 commit 4e42702

File tree

4 files changed

+36
-10
lines changed

4 files changed

+36
-10
lines changed

site/src/api/api.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { DeploymentConfig } from "./types"
55
import * as TypesGen from "./typesGenerated"
66
import { delay } from "utils/delay"
77
import userAgentParser from "ua-parser-js"
8+
// import { embedRedirect } from "../utils/redirect"
89

910
// Adds 304 for the default axios validateStatus function
1011
// https://github.com/axios/axios#handling-errors Check status here
@@ -13,14 +14,19 @@ axios.defaults.validateStatus = (status) => {
1314
return (status >= 200 && status < 300) || status === 304
1415
}
1516

16-
axios.interceptors.response.use((response) => {
17-
// If we encountered an authentication error, reset the app back to the /login route
18-
if (response.status === 401) {
19-
location.pathname = "/login"
20-
}
17+
// axios.interceptors.response.use(
18+
// (okResponse) => okResponse,
19+
// (error) => {
20+
// // 401 Unauthorized
21+
// // If we encountered an authentication error, reset the app back to the /login route
22+
// if (error.response.status === 401 && location.pathname != "/login") {
23+
// location.href = embedRedirect(`${location.pathname}${location.search}`)
24+
// }
2125

22-
return response
23-
})
26+
// // Otherwise, pass the response through so that it can be displayed in the UI
27+
// return Promise.reject(error)
28+
// },
29+
// )
2430

2531
export const hardCodedCSRFCookie = (): string => {
2632
// This is a hard coded CSRF token/cookie pair for local development. In prod,

site/src/api/errors.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,6 @@ export const getErrorDetail = (error: unknown): string | undefined | null =>
8888
: error instanceof Error
8989
? `Please check the developer console for more details.`
9090
: null
91+
92+
export const isAuthenticationError = (error: unknown): boolean =>
93+
isApiError(error) && error.response.status === 401

site/src/components/Alert/ErrorAlert.tsx

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,28 @@
11
import { AlertProps, Alert, AlertDetail } from "./Alert"
22
import AlertTitle from "@mui/material/AlertTitle"
3-
import { getErrorMessage, getErrorDetail } from "api/errors"
4-
import { FC } from "react"
3+
import {
4+
getErrorMessage,
5+
getErrorDetail,
6+
isAuthenticationError,
7+
} from "api/errors"
8+
import { useAuth } from "components/AuthProvider/AuthProvider"
9+
import { FC, useEffect } from "react"
510

611
export const ErrorAlert: FC<
712
Omit<AlertProps, "severity" | "children"> & { error: unknown }
813
> = ({ error, ...alertProps }) => {
14+
const [auth, authSend] = useAuth()
15+
const shouldSignOut = isAuthenticationError(error)
16+
useEffect(() => {
17+
if (shouldSignOut) {
18+
authSend("SIGN_OUT")
19+
}
20+
}, [shouldSignOut])
21+
22+
if (shouldSignOut) {
23+
return null
24+
}
25+
926
const message = getErrorMessage(error, "Something went wrong.")
1027
const detail = getErrorDetail(error)
1128

site/src/xServices/auth/authXService.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ export const authMachine =
331331
onError: [
332332
{
333333
actions: "assignError",
334-
target: "signedIn",
334+
target: "signedOut",
335335
},
336336
],
337337
},

0 commit comments

Comments
 (0)