-
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
1 parent
2f8c02b
commit 2c7ee00
Showing
5 changed files
with
388 additions
and
250 deletions.
There are no files selected for viewing
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
250 changes: 0 additions & 250 deletions
250
packages/huma-web-shared/src/hooks/useAuthErrorHandling.ts
This file was deleted.
Oops, something went wrong.
89 changes: 89 additions & 0 deletions
89
packages/huma-web-shared/src/hooks/useAuthErrorHandling/index.ts
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 |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import { CHAIN_TYPE } from '@huma-finance/shared' | ||
import axios, { HttpStatusCode } from 'axios' | ||
import { useCallback, useState } from 'react' | ||
import { useAuthErrorHandingEvm } from './useAuthErrorHandingEvm' | ||
import { useAuthErrorHandingSolana } from './useAuthErrorHandingSolana' | ||
|
||
export type ErrorType = 'NotSignedIn' | 'UserRejected' | 'Other' | ||
|
||
export type AuthState = { | ||
isWalletOwnershipVerificationRequired: boolean | ||
isWalletOwnershipVerified: boolean | ||
errorType?: ErrorType | ||
error: unknown | ||
setError: React.Dispatch<React.SetStateAction<unknown>> | ||
reset: () => void | ||
} | ||
|
||
export const useAuthErrorHandling = ( | ||
isDev: boolean, | ||
chainType: CHAIN_TYPE = CHAIN_TYPE.EVM, | ||
): AuthState => { | ||
const [error, setError] = useState<unknown>(null) | ||
const [isVerified, setIsVerified] = useState<boolean>(false) | ||
const [errorType, setErrorType] = useState<ErrorType | undefined>() | ||
const [isVerificationRequired, setIsVerificationRequired] = | ||
useState<boolean>(false) | ||
|
||
const handleVerificationCompletion = useCallback(() => { | ||
setIsVerified(true) | ||
}, []) | ||
|
||
const getErrorInfo = useCallback((error: any) => { | ||
const isUnauthorizedError = | ||
axios.isAxiosError(error) && | ||
error.response?.status === HttpStatusCode.Unauthorized && | ||
[ | ||
'IdTokenNotFoundException', | ||
'InvalidIdTokenException', | ||
'WalletMismatchException', | ||
].includes(error.response?.data?.detail?.type) | ||
|
||
const isWalletNotCreatedError = error === 'WalletNotCreatedException' | ||
const isWalletNotSignInError = error === 'WalletNotSignInException' | ||
|
||
return { | ||
isUnauthorizedError, | ||
isWalletNotCreatedError, | ||
isWalletNotSignInError, | ||
} | ||
}, []) | ||
|
||
useAuthErrorHandingEvm( | ||
chainType, | ||
isDev, | ||
error, | ||
getErrorInfo, | ||
setError, | ||
setErrorType, | ||
setIsVerificationRequired, | ||
handleVerificationCompletion, | ||
) | ||
useAuthErrorHandingSolana( | ||
chainType, | ||
isDev, | ||
error, | ||
getErrorInfo, | ||
setError, | ||
setErrorType, | ||
setIsVerificationRequired, | ||
handleVerificationCompletion, | ||
) | ||
|
||
const reset = useCallback(() => { | ||
setIsVerificationRequired(false) | ||
setIsVerified(false) | ||
setError(null) | ||
setErrorType(undefined) | ||
}, []) | ||
|
||
return { | ||
isWalletOwnershipVerificationRequired: isVerificationRequired, | ||
isWalletOwnershipVerified: isVerified, | ||
errorType, | ||
error, | ||
setError, | ||
reset, | ||
} | ||
} |
Oops, something went wrong.