-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
138 additions
and
184 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 53 additions & 39 deletions
92
apps/admin/src/components/ErrorBoundary/AuthErrorBoundary.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,62 @@ | ||
import { BooltiHTTPError, LOCAL_STORAGE } from '@boolti/api'; | ||
import React from 'react'; | ||
import { Navigate } from 'react-router-dom'; | ||
import { | ||
BooltiHttpError, | ||
BooltiHttpErrorParams, | ||
LOCAL_STORAGE, | ||
checkIsAuthError, | ||
checkIsHttpError, | ||
} from '@boolti/api'; | ||
import { useNavigate } from 'react-router-dom'; | ||
|
||
import { PATH } from '../../constants/routes'; | ||
|
||
interface AuthErrorBoundaryProps { | ||
children?: React.ReactNode; | ||
} | ||
|
||
interface AuthErrorBoundaryState { | ||
status: BooltiHTTPError['status'] | null; | ||
} | ||
|
||
const initialState: AuthErrorBoundaryState = { | ||
status: null, | ||
}; | ||
|
||
class AuthErrorBoundary extends React.Component<AuthErrorBoundaryProps, AuthErrorBoundaryState> { | ||
public state: AuthErrorBoundaryState = initialState; | ||
|
||
public static getDerivedStateFromError(error: Error): AuthErrorBoundaryState { | ||
if (error instanceof BooltiHTTPError) { | ||
return { | ||
status: error.status, | ||
}; | ||
} | ||
|
||
return { | ||
status: null, | ||
import { ErrorBoundary, FallbackProps } from 'react-error-boundary'; | ||
import { checkIsWebView, isWebViewBridgeAvailable, requestToken } from '@boolti/bridge'; | ||
import { useEffect } from 'react'; | ||
|
||
const AuthErrorFallback = ({ error, resetErrorBoundary }: FallbackProps) => { | ||
const navigate = useNavigate(); | ||
|
||
useEffect(() => { | ||
const reset = async () => { | ||
if (checkIsAuthError(error)) { | ||
if (checkIsWebView() && isWebViewBridgeAvailable()) { | ||
const token = (await requestToken()).data.token; | ||
localStorage.setItem(LOCAL_STORAGE.ACCESS_TOKEN, token); | ||
resetErrorBoundary(); | ||
} else { | ||
navigate(PATH.LOGIN, { replace: true }); | ||
} | ||
} else { | ||
if (checkIsHttpError(error)) { | ||
let customOptions: BooltiHttpErrorParams['customOptions']; | ||
try { | ||
const body = await error.response.json(); | ||
customOptions = { | ||
errorTraceId: body.errorTraceId, | ||
type: body.type, | ||
detail: body.detail, | ||
}; | ||
} catch { | ||
throw new BooltiHttpError({ | ||
request: error.request, | ||
response: error.response, | ||
options: error.options, | ||
customOptions, | ||
}); | ||
} | ||
} | ||
navigate(PATH.HOME, { replace: true }); | ||
} | ||
}; | ||
} | ||
|
||
public render() { | ||
if (this.state.status !== null) { | ||
this.setState(initialState); | ||
|
||
window.localStorage.removeItem(LOCAL_STORAGE.ACCESS_TOKEN); | ||
window.localStorage.removeItem(LOCAL_STORAGE.REFRESH_TOKEN); | ||
reset(); | ||
}, []); | ||
Check warning on line 53 in apps/admin/src/components/ErrorBoundary/AuthErrorBoundary.tsx GitHub Actions / Build and Test
|
||
|
||
return <Navigate to={PATH.LOGIN} replace />; | ||
} | ||
return null; | ||
}; | ||
|
||
return this.props.children; | ||
} | ||
} | ||
const AuthErrorBoundary = ({ children }: React.PropsWithChildren) => { | ||
return <ErrorBoundary FallbackComponent={AuthErrorFallback}>{children}</ErrorBoundary>; | ||
}; | ||
|
||
export default AuthErrorBoundary; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import { ERROR_CODE } from './errorCode'; | ||
import { LOCAL_STORAGE, COOKIES } from './storages'; | ||
import { LOCAL_STORAGE } from './storages'; | ||
|
||
export { ERROR_CODE, LOCAL_STORAGE, COOKIES }; | ||
export { ERROR_CODE, LOCAL_STORAGE }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.