@agg-build/auth 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,251 @@
1
+ # @agg-build/auth
2
+
3
+ Modular connect / sign-in UI and auth-method adapters for the
4
+ [AGG](https://docs.agg.market/) prediction market aggregator. Drop in a `ConnectButton`, pick the
5
+ auth methods you want to enable (SIWE, SIWS, Google, Apple, Twitter/X, email magic-link), and ship.
6
+
7
+ ## What it is
8
+
9
+ `@agg-build/auth` keeps provider-specific auth integrations out of
10
+ [`@agg-build/ui`](https://www.npmjs.com/package/@agg-build/ui). Each auth method ships as a separate adapter
11
+ — wallet providers (SIWE, SIWS) stay behind subpath imports so their peer dependencies are only
12
+ pulled in when you actually enable them. `AggAuthProvider` orchestrates the flow end-to-end:
13
+ opening the chooser, driving wallet sign-in, redirecting to OAuth providers, emailing magic
14
+ links, handling the post-redirect code exchange, and transparently solving Cloudflare Turnstile
15
+ challenges when the API requires them.
16
+
17
+ ## When to use this package
18
+
19
+ - You want AGG's connect / sign-in UX without rebuilding it.
20
+ - You want to enable only the auth methods your app supports.
21
+ - You want redirect callback handling (OAuth + email magic-link) solved for you.
22
+ - You want Cloudflare Turnstile challenge handling built in.
23
+
24
+ If you prefer to roll your own wallet UI, skip this package and use `useAggAuth` from
25
+ [`@agg-build/hooks`](https://www.npmjs.com/package/@agg-build/hooks) directly.
26
+
27
+ ## Install
28
+
29
+ Core packages:
30
+
31
+ ```bash
32
+ npm install @agg-build/sdk @agg-build/hooks @agg-build/ui @agg-build/auth liveline
33
+ ```
34
+
35
+ Add only the optional peers you need:
36
+
37
+ ```bash
38
+ # SIWE / Ethereum
39
+ npm install wagmi viem
40
+
41
+ # SIWS / Solana
42
+ npm install @solana/wallet-adapter-react bs58
43
+ ```
44
+
45
+ Google, Apple, Twitter/X, and email magic-link flows don't require any extra peers — they are
46
+ plain redirect-based flows driven by the SDK.
47
+
48
+ ## Quick start
49
+
50
+ ```tsx
51
+ import "@agg-build/ui/styles.css";
52
+ import { AggProvider } from "@agg-build/hooks";
53
+ import { createAggClient } from "@agg-build/sdk";
54
+ import {
55
+ AggAuthProvider,
56
+ ConnectButton,
57
+ createAppleAuthMethod,
58
+ createEmailAuthMethod,
59
+ createGoogleAuthMethod,
60
+ createTwitterAuthMethod,
61
+ } from "@agg-build/auth";
62
+ import { useSiweAuthMethod } from "@agg-build/auth/siwe";
63
+ import { useSiwsAuthMethod } from "@agg-build/auth/siws";
64
+ import { WagmiProvider } from "wagmi";
65
+ import { wagmiConfig } from "./wagmi-config";
66
+
67
+ const client = createAggClient({
68
+ appId: "your-app-id",
69
+ baseUrl: "https://api.agg.market",
70
+ });
71
+
72
+ function AuthActions() {
73
+ const siwe = useSiweAuthMethod({ statement: "Sign in to AGG" });
74
+ const siws = useSiwsAuthMethod({ cluster: "mainnet", statement: "Sign in to AGG" });
75
+
76
+ return (
77
+ <AggAuthProvider
78
+ methods={[
79
+ siwe,
80
+ siws,
81
+ createGoogleAuthMethod(),
82
+ createAppleAuthMethod(),
83
+ createTwitterAuthMethod(),
84
+ createEmailAuthMethod(),
85
+ ]}
86
+ >
87
+ <ConnectButton />
88
+ </AggAuthProvider>
89
+ );
90
+ }
91
+
92
+ export default function App() {
93
+ return (
94
+ <WagmiProvider config={wagmiConfig}>
95
+ <AggProvider client={client}>
96
+ <AuthActions />
97
+ </AggProvider>
98
+ </WagmiProvider>
99
+ );
100
+ }
101
+ ```
102
+
103
+ `AggAuthProvider` watches the current URL on mount. For redirect-based methods it reads the
104
+ `code` query parameter, calls `client.exchangeAuthCode(code)`, then cleans the URL. No extra
105
+ setup required if the user lands back on the same app.
106
+
107
+ ## Dedicated callback route
108
+
109
+ If your app routes redirects to a separate callback page, hydrate the exchange yourself with
110
+ `useAggAuthCallback`:
111
+
112
+ ```tsx
113
+ import { useAggAuthCallback } from "@agg-build/auth";
114
+
115
+ export function AuthCallbackPage() {
116
+ const { error, isHandled, isHandling, user } = useAggAuthCallback();
117
+
118
+ if (isHandling) return <p>Finishing sign-in…</p>;
119
+ if (error) return <p>{error.message}</p>;
120
+ if (isHandled) return <p>Signed in as {user?.id}</p>;
121
+
122
+ return <p>No AGG auth callback data was found.</p>;
123
+ }
124
+ ```
125
+
126
+ The callback hook follows the same recipe:
127
+
128
+ 1. Parse the `code` query parameter.
129
+ 2. Call `client.exchangeAuthCode(code)`.
130
+ 3. Remove `code` from the URL via `window.history.replaceState(...)`.
131
+
132
+ ## Turnstile challenges
133
+
134
+ When the AGG API returns `challenge_required`, `AggAuthProvider` automatically renders a
135
+ Cloudflare Turnstile widget, submits the solved token, and retries the request. The widget is
136
+ invisible on success and degrades gracefully if a user's environment blocks Turnstile. You do not
137
+ need to render or configure the widget directly.
138
+
139
+ ## Available auth methods
140
+
141
+ | Import path | Export | Peer deps | Notes |
142
+ | ---------------------- | --------------------------- | -------------------------------------- | ---------------------------------------- |
143
+ | `@agg-build/auth` | `createEmailAuthMethod()` | — | Magic-link email flow |
144
+ | `@agg-build/auth` | `createGoogleAuthMethod()` | — | Redirect-based OAuth |
145
+ | `@agg-build/auth` | `createAppleAuthMethod()` | — | Redirect-based OAuth |
146
+ | `@agg-build/auth` | `createTwitterAuthMethod()` | — | Redirect-based OAuth |
147
+ | `@agg-build/auth/siwe` | `useSiweAuthMethod()` | `wagmi` | Ethereum wallet sign-in |
148
+ | `@agg-build/auth/siws` | `useSiwsAuthMethod()` | `@solana/wallet-adapter-react`, `bs58` | Solana wallet sign-in; Ledger-compatible |
149
+
150
+ Pass the methods you want to enable to `AggAuthProvider` — the `ConnectButton` chooser only lists
151
+ methods you include.
152
+
153
+ ### Custom redirect URL / navigation
154
+
155
+ Redirect-based adapters accept a `redirectUrl` and optional `navigate` function:
156
+
157
+ ```tsx
158
+ createGoogleAuthMethod({
159
+ redirectUrl: "https://yourapp.com/auth/callback",
160
+ navigate: (url) => window.top?.location.assign(url),
161
+ });
162
+ ```
163
+
164
+ ### Custom wallet picker (SIWE)
165
+
166
+ `useSiweAuthMethod` accepts an optional `connectorPicker` to render your own wallet chooser modal
167
+ instead of `wagmi`'s default:
168
+
169
+ ```tsx
170
+ const siwe = useSiweAuthMethod({
171
+ connectorPicker: async (connectors) => showMyWalletModal(connectors),
172
+ });
173
+ ```
174
+
175
+ ## `ConnectButton` props
176
+
177
+ | Prop | Type | Description |
178
+ | -------------------- | --------------------------------------------------- | -------------------------------------------- |
179
+ | `buttonProps` | `ConnectButtonButtonProps` | Props passed to the inner `Button` component |
180
+ | `classNames` | `ConnectButtonClassNames` | Style overrides for sub-elements |
181
+ | `disabled` | `boolean` | Disable the button |
182
+ | `onAuthMethodSelect` | `(methodId: AuthMethodId) => void \| Promise<void>` | Called when user picks an auth method |
183
+ | `onDepositClick` | `() => void \| Promise<void>` | Called when user clicks deposit |
184
+ | `onDisconnect` | `() => void \| Promise<void>` | Called on disconnect |
185
+ | `onProfileClick` | `() => void \| Promise<void>` | Called when user clicks profile |
186
+ | `onWithdrawClick` | `() => void \| Promise<void>` | Called when user clicks withdraw |
187
+
188
+ When authenticated, the button collapses into the user's profile chip with action buttons
189
+ (deposit, withdraw, profile, disconnect). Use the callback props to wire them to your app's
190
+ navigation or modal system.
191
+
192
+ ## Main exports
193
+
194
+ | Import path | Export | Description |
195
+ | ---------------------- | ------------------------- | --------------------------------------------------------------- |
196
+ | `@agg-build/auth` | `AggAuthProvider` | Orchestrates the chooser, redirect callback, and Turnstile |
197
+ | `@agg-build/auth` | `ConnectButton` | Drop-in connect / account chip |
198
+ | `@agg-build/auth` | `useAggAuthFlow` | Access the chooser/flow state (used by custom UIs) |
199
+ | `@agg-build/auth` | `useAggAuthCallback` | Manually hydrate the redirect callback page |
200
+ | `@agg-build/auth` | `createEmailAuthMethod` | Magic-link email adapter |
201
+ | `@agg-build/auth` | `createGoogleAuthMethod` | Google OAuth adapter |
202
+ | `@agg-build/auth` | `createAppleAuthMethod` | Apple OAuth adapter |
203
+ | `@agg-build/auth` | `createTwitterAuthMethod` | Twitter/X OAuth adapter |
204
+ | `@agg-build/auth/siwe` | `useSiweAuthMethod` | SIWE adapter (requires `wagmi`) |
205
+ | `@agg-build/auth/siws` | `useSiwsAuthMethod` | SIWS adapter (requires `@solana/wallet-adapter-react` + `bs58`) |
206
+
207
+ ## Architecture
208
+
209
+ - `@agg-build/sdk` owns auth API calls and session-token storage.
210
+ - `@agg-build/hooks` owns shared auth/session state inside `AggProvider`.
211
+ - `@agg-build/ui` ships generic UI primitives only.
212
+ - `@agg-build/auth` ships auth-method adapters + the connect/sign-in UX.
213
+
214
+ ## Entrypoints
215
+
216
+ | Entry | Purpose |
217
+ | -------------------------------- | ---------------------------------------------------------------------------- |
218
+ | `@agg-build/auth` | `AggAuthProvider`, `ConnectButton`, OAuth + email adapters |
219
+ | `@agg-build/auth/connect-button` | `ConnectButton` component only (narrow import) |
220
+ | `@agg-build/auth/oauth` | `createAppleAuthMethod`, `createGoogleAuthMethod`, `createTwitterAuthMethod` |
221
+ | `@agg-build/auth/email` | `createEmailAuthMethod` |
222
+ | `@agg-build/auth/siwe` | `useSiweAuthMethod` (requires `wagmi`) |
223
+ | `@agg-build/auth/siws` | `useSiwsAuthMethod` (requires Solana wallet adapter) |
224
+
225
+ ## Peer dependencies
226
+
227
+ Required:
228
+
229
+ - `@agg-build/hooks`
230
+ - `@agg-build/ui`
231
+ - `react` ^18.0.0 or ^19.0.0
232
+ - `react-dom` ^18.0.0 or ^19.0.0
233
+
234
+ Optional (tied to specific adapters):
235
+
236
+ - `wagmi` ^3.5.0 — SIWE
237
+ - `@solana/wallet-adapter-react` ^0.15.39 — SIWS
238
+ - `bs58` ^6.0.0 — SIWS signature encoding
239
+
240
+ ## Links
241
+
242
+ - Documentation — <https://docs.agg.market/>
243
+ - Storybook — <https://storybook.agg.market/>
244
+ - Demo app — <https://demo.agg.market/>
245
+ - Vanilla SDK — [`@agg-build/sdk`](https://www.npmjs.com/package/@agg-build/sdk)
246
+ - React hooks — [`@agg-build/hooks`](https://www.npmjs.com/package/@agg-build/hooks)
247
+ - Trading UI — [`@agg-build/ui`](https://www.npmjs.com/package/@agg-build/ui)
248
+
249
+ ## License
250
+
251
+ MIT