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