Skip to content

Commit 26ff66a

Browse files
committed
oh yeah, this works super nice
1 parent bcbf937 commit 26ff66a

File tree

2 files changed

+24
-17
lines changed

2 files changed

+24
-17
lines changed

site/src/api/api.ts

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ 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"
98

109
// Adds 304 for the default axios validateStatus function
1110
// https://github.com/axios/axios#handling-errors Check status here
@@ -14,20 +13,6 @@ axios.defaults.validateStatus = (status) => {
1413
return (status >= 200 && status < 300) || status === 304
1514
}
1615

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-
// }
25-
26-
// // Otherwise, pass the response through so that it can be displayed in the UI
27-
// return Promise.reject(error)
28-
// },
29-
// )
30-
3116
export const hardCodedCSRFCookie = (): string => {
3217
// This is a hard coded CSRF token/cookie pair for local development. In prod,
3318
// the GoLang webserver generates a random cookie with a new token for each

site/src/components/RequireAuth/RequireAuth.tsx

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,41 @@
1+
import axios from "axios"
12
import { useAuth } from "components/AuthProvider/AuthProvider"
2-
import { FC } from "react"
3+
import { FC, useEffect } from "react"
34
import { Outlet, Navigate, useLocation } from "react-router-dom"
45
import { embedRedirect } from "../../utils/redirect"
56
import { FullScreenLoader } from "../Loader/FullScreenLoader"
67
import { DashboardProvider } from "components/Dashboard/DashboardProvider"
78
import { ProxyProvider } from "contexts/ProxyContext"
89

910
export const RequireAuth: FC = () => {
10-
const [authState] = useAuth()
11+
const [authState, authSend] = useAuth()
1112
const location = useLocation()
1213
const isHomePage = location.pathname === "/"
1314
const navigateTo = isHomePage
1415
? "/login"
1516
: embedRedirect(`${location.pathname}${location.search}`)
1617

18+
useEffect(() => {
19+
const interceptorHandle = axios.interceptors.response.use(
20+
(okResponse) => okResponse,
21+
(error) => {
22+
// 401 Unauthorized
23+
// If we encountered an authentication error, then our token is probably
24+
// invalid and we should update the auth state to reflect that.
25+
if (error.response.status === 401) {
26+
authSend("SIGN_OUT")
27+
}
28+
29+
// Otherwise, pass the response through so that it can be displayed in the UI
30+
return Promise.reject(error)
31+
},
32+
)
33+
34+
return () => {
35+
axios.interceptors.response.eject(interceptorHandle)
36+
}
37+
}, [])
38+
1739
if (authState.matches("signedOut")) {
1840
return <Navigate to={navigateTo} state={{ isRedirect: !isHomePage }} />
1941
} else if (authState.matches("configuringTheFirstUser")) {

0 commit comments

Comments
 (0)