|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * React hooks for managing OAuth/OIDC client applications. |
| 5 | + * |
| 6 | + * Wraps `client.oauth.applications.*` (better-auth `oidc-provider` plugin). |
| 7 | + * Used by the OAuth Apps pages under /account/oauth-applications. |
| 8 | + */ |
| 9 | + |
| 10 | +import { useCallback, useEffect, useState } from 'react'; |
| 11 | +import { useClient } from '@objectstack/client-react'; |
| 12 | + |
| 13 | +export interface OAuthApplication { |
| 14 | + id: string; |
| 15 | + name: string; |
| 16 | + client_id: string; |
| 17 | + client_secret?: string | null; |
| 18 | + redirect_urls: string; |
| 19 | + type: 'web' | 'native' | 'user-agent-based' | 'public'; |
| 20 | + disabled?: boolean; |
| 21 | + icon?: string | null; |
| 22 | + metadata?: string | null; |
| 23 | + user_id?: string | null; |
| 24 | + created_at?: string; |
| 25 | + updated_at?: string; |
| 26 | +} |
| 27 | + |
| 28 | +/** |
| 29 | + * Hook: list the current user's OAuth client applications. |
| 30 | + */ |
| 31 | +export function useOAuthApplications() { |
| 32 | + const client = useClient() as any; |
| 33 | + const [applications, setApplications] = useState<OAuthApplication[]>([]); |
| 34 | + const [loading, setLoading] = useState(false); |
| 35 | + const [error, setError] = useState<Error | null>(null); |
| 36 | + |
| 37 | + const reload = useCallback(async () => { |
| 38 | + if (!client?.oauth?.applications) return; |
| 39 | + setLoading(true); |
| 40 | + setError(null); |
| 41 | + try { |
| 42 | + const res = await client.oauth.applications.list(); |
| 43 | + setApplications(res?.applications ?? []); |
| 44 | + } catch (err) { |
| 45 | + setError(err as Error); |
| 46 | + setApplications([]); |
| 47 | + } finally { |
| 48 | + setLoading(false); |
| 49 | + } |
| 50 | + }, [client]); |
| 51 | + |
| 52 | + useEffect(() => { |
| 53 | + reload(); |
| 54 | + }, [reload]); |
| 55 | + |
| 56 | + return { applications, loading, error, reload }; |
| 57 | +} |
| 58 | + |
| 59 | +/** |
| 60 | + * Hook: register a new OAuth client application. |
| 61 | + * |
| 62 | + * Returns `{ register, registering, error, lastResult }` where `lastResult` |
| 63 | + * holds the response of the most recent successful registration — including |
| 64 | + * the freshly issued `client_secret`, which is only returned once. |
| 65 | + */ |
| 66 | +export function useRegisterOAuthApplication() { |
| 67 | + const client = useClient() as any; |
| 68 | + const [registering, setRegistering] = useState(false); |
| 69 | + const [error, setError] = useState<Error | null>(null); |
| 70 | + const [lastResult, setLastResult] = useState<any>(null); |
| 71 | + |
| 72 | + const register = useCallback( |
| 73 | + async (req: { |
| 74 | + client_name: string; |
| 75 | + redirect_uris: string[]; |
| 76 | + token_endpoint_auth_method?: 'none' | 'client_secret_basic' | 'client_secret_post'; |
| 77 | + grant_types?: string[]; |
| 78 | + response_types?: string[]; |
| 79 | + client_uri?: string; |
| 80 | + logo_uri?: string; |
| 81 | + scope?: string; |
| 82 | + contacts?: string[]; |
| 83 | + tos_uri?: string; |
| 84 | + policy_uri?: string; |
| 85 | + metadata?: Record<string, unknown>; |
| 86 | + }) => { |
| 87 | + if (!client?.oauth?.applications?.register) throw new Error('Client not ready'); |
| 88 | + setRegistering(true); |
| 89 | + setError(null); |
| 90 | + try { |
| 91 | + const result = await client.oauth.applications.register(req); |
| 92 | + setLastResult(result); |
| 93 | + return result; |
| 94 | + } catch (err) { |
| 95 | + setError(err as Error); |
| 96 | + throw err; |
| 97 | + } finally { |
| 98 | + setRegistering(false); |
| 99 | + } |
| 100 | + }, |
| 101 | + [client], |
| 102 | + ); |
| 103 | + |
| 104 | + return { register, registering, error, lastResult }; |
| 105 | +} |
| 106 | + |
| 107 | +/** |
| 108 | + * Hook: delete (revoke) an OAuth client application by row id. |
| 109 | + */ |
| 110 | +export function useDeleteOAuthApplication() { |
| 111 | + const client = useClient() as any; |
| 112 | + const [deleting, setDeleting] = useState(false); |
| 113 | + const [error, setError] = useState<Error | null>(null); |
| 114 | + |
| 115 | + const remove = useCallback( |
| 116 | + async (id: string) => { |
| 117 | + if (!client?.oauth?.applications?.delete) throw new Error('Client not ready'); |
| 118 | + setDeleting(true); |
| 119 | + setError(null); |
| 120 | + try { |
| 121 | + return await client.oauth.applications.delete(id); |
| 122 | + } catch (err) { |
| 123 | + setError(err as Error); |
| 124 | + throw err; |
| 125 | + } finally { |
| 126 | + setDeleting(false); |
| 127 | + } |
| 128 | + }, |
| 129 | + [client], |
| 130 | + ); |
| 131 | + |
| 132 | + return { remove, deleting, error }; |
| 133 | +} |
| 134 | + |
| 135 | +/** |
| 136 | + * Hook: submit a consent decision to a pending OAuth authorization request. |
| 137 | + */ |
| 138 | +export function useOAuthConsent() { |
| 139 | + const client = useClient() as any; |
| 140 | + const [submitting, setSubmitting] = useState(false); |
| 141 | + const [error, setError] = useState<Error | null>(null); |
| 142 | + |
| 143 | + const submit = useCallback( |
| 144 | + async (req: { accept: boolean; consent_code?: string }) => { |
| 145 | + if (!client?.oauth?.consent) throw new Error('Client not ready'); |
| 146 | + setSubmitting(true); |
| 147 | + setError(null); |
| 148 | + try { |
| 149 | + return await client.oauth.consent(req); |
| 150 | + } catch (err) { |
| 151 | + setError(err as Error); |
| 152 | + throw err; |
| 153 | + } finally { |
| 154 | + setSubmitting(false); |
| 155 | + } |
| 156 | + }, |
| 157 | + [client], |
| 158 | + ); |
| 159 | + |
| 160 | + return { submit, submitting, error }; |
| 161 | +} |
0 commit comments