MCPcopy Create free account
hub / github.com/Noumena-Network/code / useApiKeyVerification

Function useApiKeyVerification

src/hooks/useApiKeyVerification.ts:68–120  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

66}
67
68export function useApiKeyVerification(): ApiKeyVerificationResult {
69 const [status, setStatus] = useState<VerificationStatus>(() => {
70 if (shouldSkipApiKeyVerification()) {
71 return 'valid'
72 }
73
74 return getInitialApiKeyVerificationStatus(
75 getAuthRuntime().getCurrentSession(),
76 )
77 })
78 const [error, setError] = useState<Error | null>(null)
79
80 const verify = useCallback(async (): Promise<void> => {
81 if (shouldSkipApiKeyVerification()) {
82 setStatus('valid')
83 return
84 }
85 const session = await resolveApiKeyVerificationSession()
86 const apiKey = session.apiKey
87 const source = session.rawApiKeySource
88 if (!apiKey) {
89 if (source === 'apiKeyHelper') {
90 setStatus('error')
91 setError(new Error('API key helper did not return a valid key'))
92 return
93 }
94 const newStatus = 'missing'
95 setStatus(newStatus)
96 return
97 }
98
99 try {
100 const isValid = await verifyApiKey(apiKey, false)
101 const newStatus = isValid ? 'valid' : 'invalid'
102 setStatus(newStatus)
103 return
104 } catch (error) {
105 // This happens when there an error response from the API but it's not an invalid API key error
106 // In this case, we still mark the API key as invalid - but we also log the error so we can
107 // display it to the user to be more helpful
108 setError(error as Error)
109 const newStatus = 'error'
110 setStatus(newStatus)
111 return
112 }
113 }, [])
114
115 return {
116 status,
117 reverify: verify,
118 error,
119 }
120}

Callers 1

REPLFunction · 0.85

Calls 7

getAuthRuntimeFunction · 0.85
setStatusFunction · 0.85
verifyApiKeyFunction · 0.85
getCurrentSessionMethod · 0.65

Tested by

no test coverage detected