@auth-gate/react 0.1.0 → 0.2.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.
Files changed (2) hide show
  1. package/README.md +241 -0
  2. package/package.json +2 -2
package/README.md ADDED
@@ -0,0 +1,241 @@
1
+ # @auth-gate/react
2
+
3
+ React SDK for [AuthGate](https://authgate.dev) — drop-in authentication with OAuth, email, SMS, and MFA for React apps using Hono as the backend.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @auth-gate/react
9
+ ```
10
+
11
+ **Peer dependencies:** `react >= 18`, `hono >= 4`
12
+
13
+ ## Quick Start
14
+
15
+ ### 1. Create Auth Routes (Server)
16
+
17
+ ```ts
18
+ // server/auth.ts
19
+ import { createAuthRoutes } from "@auth-gate/react/server";
20
+
21
+ const auth = createAuthRoutes({
22
+ apiKey: process.env.AUTHGATE_API_KEY!,
23
+ projectId: process.env.AUTHGATE_PROJECT_ID!,
24
+ baseUrl: "https://authgate.dev",
25
+ sessionSecret: process.env.SESSION_SECRET!,
26
+ appUrl: "http://localhost:3000",
27
+ });
28
+ ```
29
+
30
+ ### 2. Mount in Hono
31
+
32
+ ```ts
33
+ // server/index.ts
34
+ import { Hono } from "hono";
35
+ import { auth } from "./auth";
36
+
37
+ const app = new Hono();
38
+ app.route("/api/auth", auth);
39
+
40
+ export default app;
41
+ ```
42
+
43
+ ### 3. Add the Provider (Client)
44
+
45
+ ```tsx
46
+ // src/App.tsx
47
+ import { AuthProvider } from "@auth-gate/react";
48
+
49
+ export default function App() {
50
+ return (
51
+ <AuthProvider>
52
+ <Router />
53
+ </AuthProvider>
54
+ );
55
+ }
56
+ ```
57
+
58
+ ### 4. Use the Hooks
59
+
60
+ ```tsx
61
+ import { useAuth } from "@auth-gate/react";
62
+
63
+ function Profile() {
64
+ const { user, loading, logout } = useAuth();
65
+
66
+ if (loading) return <p>Loading...</p>;
67
+ if (!user) return <p>Not signed in</p>;
68
+
69
+ return (
70
+ <div>
71
+ <p>Hello, {user.name}</p>
72
+ <button onClick={logout}>Sign out</button>
73
+ </div>
74
+ );
75
+ }
76
+ ```
77
+
78
+ ## Route Map
79
+
80
+ The Hono router registers these routes under your mount path (e.g. `/api/auth/`):
81
+
82
+ | Method | Route | Description |
83
+ |--------|-------|-------------|
84
+ | GET | `/:provider/login` | Start OAuth flow (google, github, discord, azure, apple) |
85
+ | GET | `/callback` | OAuth / magic link callback |
86
+ | POST | `/email/signup` | Email registration |
87
+ | POST | `/email/signin` | Email sign-in |
88
+ | POST | `/email/forgot-password` | Request password reset |
89
+ | POST | `/email/reset-password` | Confirm password reset |
90
+ | POST | `/email/verify-code` | Verify email with OTP |
91
+ | POST | `/magic-link/send` | Send magic link |
92
+ | POST | `/sms/send-code` | Send SMS code |
93
+ | POST | `/sms/verify-code` | Verify SMS code |
94
+ | POST | `/mfa/verify` | Complete MFA challenge |
95
+ | POST | `/refresh` | Refresh session token |
96
+ | POST | `/logout` | Sign out and revoke session |
97
+ | GET | `/me` | Get current user |
98
+
99
+ ## Client Components
100
+
101
+ ### `<AuthProvider>`
102
+
103
+ Wraps your app and manages auth state via context.
104
+
105
+ ```tsx
106
+ <AuthProvider
107
+ meEndpoint="/api/auth/me" // default
108
+ logoutEndpoint="/api/auth/logout" // default
109
+ >
110
+ {children}
111
+ </AuthProvider>
112
+ ```
113
+
114
+ | Prop | Type | Default | Description |
115
+ |------|------|---------|-------------|
116
+ | `meEndpoint` | `string` | `"/api/auth/me"` | Endpoint to fetch current user |
117
+ | `logoutEndpoint` | `string` | `"/api/auth/logout"` | Endpoint to call on logout |
118
+
119
+ ### `<AuthGuard>`
120
+
121
+ Protects routes by redirecting unauthenticated users.
122
+
123
+ ```tsx
124
+ import { AuthGuard } from "@auth-gate/react";
125
+
126
+ function ProtectedPage() {
127
+ return (
128
+ <AuthGuard redirectTo="/login" loadingFallback={<Spinner />}>
129
+ <Dashboard />
130
+ </AuthGuard>
131
+ );
132
+ }
133
+ ```
134
+
135
+ | Prop | Type | Default | Description |
136
+ |------|------|---------|-------------|
137
+ | `redirectTo` | `string` | `"/login"` | Redirect path when not authenticated |
138
+ | `loadingFallback` | `ReactNode` | — | Shown while session is loading |
139
+
140
+ ### Hooks
141
+
142
+ #### `useAuth()`
143
+
144
+ Returns the full auth context.
145
+
146
+ ```ts
147
+ const { user, loading, refresh, logout } = useAuth();
148
+ ```
149
+
150
+ | Property | Type | Description |
151
+ |----------|------|-------------|
152
+ | `user` | `AuthGateUser \| null` | Current user or null |
153
+ | `loading` | `boolean` | True while fetching session |
154
+ | `refresh` | `() => Promise<void>` | Re-fetch the session |
155
+ | `logout` | `() => Promise<void>` | Sign out and clear state |
156
+
157
+ #### `useSession()`
158
+
159
+ Shorthand that returns just the user.
160
+
161
+ ```ts
162
+ const user = useSession(); // AuthGateUser | null
163
+ ```
164
+
165
+ ## Authentication Examples
166
+
167
+ ### OAuth Sign-In
168
+
169
+ ```html
170
+ <a href="/api/auth/google/login">Sign in with Google</a>
171
+ <a href="/api/auth/github/login">Sign in with GitHub</a>
172
+ ```
173
+
174
+ ### Email Sign-In
175
+
176
+ ```ts
177
+ const res = await fetch("/api/auth/email/signin", {
178
+ method: "POST",
179
+ headers: { "Content-Type": "application/json" },
180
+ body: JSON.stringify({ email, password }),
181
+ });
182
+
183
+ const data = await res.json();
184
+
185
+ if (data.mfa_required) {
186
+ await fetch("/api/auth/mfa/verify", {
187
+ method: "POST",
188
+ headers: { "Content-Type": "application/json" },
189
+ body: JSON.stringify({
190
+ mfa_challenge: data.mfa_challenge,
191
+ code: totpCode,
192
+ method: "totp",
193
+ }),
194
+ });
195
+ }
196
+ ```
197
+
198
+ ### SMS Sign-In
199
+
200
+ ```ts
201
+ await fetch("/api/auth/sms/send-code", {
202
+ method: "POST",
203
+ headers: { "Content-Type": "application/json" },
204
+ body: JSON.stringify({ phone: "+15551234567" }),
205
+ });
206
+
207
+ await fetch("/api/auth/sms/verify-code", {
208
+ method: "POST",
209
+ headers: { "Content-Type": "application/json" },
210
+ body: JSON.stringify({ phone: "+15551234567", code: "123456" }),
211
+ });
212
+ ```
213
+
214
+ ## Server API Reference
215
+
216
+ ### `createAuthRoutes(options)`
217
+
218
+ Creates a Hono router with all auth endpoints.
219
+
220
+ | Option | Type | Required | Description |
221
+ |--------|------|----------|-------------|
222
+ | `apiKey` | `string` | Yes | AuthGate API key |
223
+ | `projectId` | `string` | Yes | AuthGate project ID |
224
+ | `baseUrl` | `string` | Yes | AuthGate instance URL |
225
+ | `sessionSecret` | `string` | Yes | Encryption secret (min 32 chars) |
226
+ | `appUrl` | `string` | Yes | Your app's URL |
227
+ | `cookieName` | `string` | No | Cookie name (default: `__authgate`) |
228
+ | `sessionMaxAge` | `number` | No | Session TTL in seconds (default: `604800`) |
229
+ | `callbackPath` | `string` | No | Callback path (default: `/api/auth/callback`) |
230
+
231
+ ## Types
232
+
233
+ ```ts
234
+ import type { AuthGateUser, OAuthProvider } from "@auth-gate/react";
235
+ ```
236
+
237
+ Re-exported from `@auth-gate/core` for convenience.
238
+
239
+ ## License
240
+
241
+ MIT
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@auth-gate/react",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {
@@ -21,7 +21,7 @@
21
21
  "dist"
22
22
  ],
23
23
  "dependencies": {
24
- "@auth-gate/core": "0.1.0"
24
+ "@auth-gate/core": "0.2.0"
25
25
  },
26
26
  "peerDependencies": {
27
27
  "react": ">=18",