Skip to content
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

solana ledger sign in #358

Merged
merged 5 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions packages/huma-shared/src/services/AuthService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,25 @@ const verifySignature = async (
},
)

const verifySolanaTx = async (
message: string,
serializedTx: string,
chainId: number,
isDev: boolean = false,
): Promise<null> =>
requestPost<null>(
`${configUtil.getAuthServiceUrl(
chainId,
isDev,
)}/verify-signature?chainId=${chainId}`,
{
message,
serializedTx,
},
)

export const AuthService = {
createSession,
verifySignature,
verifySolanaTx,
}
250 changes: 0 additions & 250 deletions packages/huma-web-shared/src/hooks/useAuthErrorHandling.ts

This file was deleted.

89 changes: 89 additions & 0 deletions packages/huma-web-shared/src/hooks/useAuthErrorHandling/index.ts
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 { useAuthErrorHandlingEvm } from './useAuthErrorHandlingEvm'
import { useAuthErrorHandlingSolana } from './useAuthErrorHandlingSolana'

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'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where are these errors coming from? Doesn't seem like they are from the backend. From a grammar perspective, the second error should be called WalletNotSignedInException.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These 2 are frontend defined errors. WalletNotCreatedException is thrown from earn page, WalletNotSignInException is thrown in points earned modal after supply.


return {
isUnauthorizedError,
isWalletNotCreatedError,
isWalletNotSignInError,
}
}, [])

useAuthErrorHandlingEvm(
chainType,
isDev,
error,
getErrorInfo,
setError,
setErrorType,
setIsVerificationRequired,
handleVerificationCompletion,
)
useAuthErrorHandlingSolana(
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,
}
}
Loading
Loading