@qyh213/easyauth-client 1.0.0-beta.1
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/LICENSE +21 -0
- package/README.md +425 -0
- package/dist/chunk-H65ZLXQJ.mjs +495 -0
- package/dist/client-RPDHpVsj.d.mts +300 -0
- package/dist/client-RPDHpVsj.d.ts +300 -0
- package/dist/index.d.mts +31 -0
- package/dist/index.d.ts +31 -0
- package/dist/index.js +530 -0
- package/dist/index.mjs +18 -0
- package/dist/next.d.mts +106 -0
- package/dist/next.d.ts +106 -0
- package/dist/next.js +638 -0
- package/dist/next.mjs +124 -0
- package/dist/react.d.mts +68 -0
- package/dist/react.d.ts +68 -0
- package/dist/react.js +733 -0
- package/dist/react.mjs +220 -0
- package/package.json +99 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Easy Auth
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,425 @@
|
|
|
1
|
+
# @easyauth/client
|
|
2
|
+
|
|
3
|
+
Official client library for Easy Auth - Multi-tenant authentication-as-a-service platform.
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/@easyauth/client)
|
|
6
|
+
[](https://opensource.org/licenses/MIT)
|
|
7
|
+
|
|
8
|
+
## Features
|
|
9
|
+
|
|
10
|
+
- 🔐 **Simple Authentication** - Login/logout with tokens
|
|
11
|
+
- 🛡️ **Token Validation** - Validate tokens with easy-auth service
|
|
12
|
+
- 👥 **User Management** - Create, list, delete users
|
|
13
|
+
- 🔑 **API Key Management** - Create and revoke service API keys
|
|
14
|
+
- ⚛️ **React Integration** - Hooks and context for React apps
|
|
15
|
+
- ▲ **Next.js Support** - Middleware and server-side helpers
|
|
16
|
+
- 📘 **TypeScript** - Full type definitions included
|
|
17
|
+
|
|
18
|
+
## Installation
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
npm install @easyauth/client
|
|
22
|
+
# or
|
|
23
|
+
yarn add @easyauth/client
|
|
24
|
+
# or
|
|
25
|
+
pnpm add @easyauth/client
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Quick Start
|
|
29
|
+
|
|
30
|
+
### 1. Initialize the Client
|
|
31
|
+
|
|
32
|
+
```typescript
|
|
33
|
+
import { EasyAuthClient } from "@easyauth/client";
|
|
34
|
+
|
|
35
|
+
const client = new EasyAuthClient({
|
|
36
|
+
baseUrl: "https://auth.yourservice.com",
|
|
37
|
+
apiKey: "ak-xxxxx", // Your service API key
|
|
38
|
+
});
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
### 2. Login a User
|
|
42
|
+
|
|
43
|
+
```typescript
|
|
44
|
+
// Login and store token
|
|
45
|
+
const token = await client.login({
|
|
46
|
+
email: "user@example.com",
|
|
47
|
+
password: "password123",
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
console.log("Logged in! Token expires:", token.expiresAt);
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### 3. Validate Token
|
|
54
|
+
|
|
55
|
+
```typescript
|
|
56
|
+
// Check if current token is valid
|
|
57
|
+
const result = await client.validate();
|
|
58
|
+
|
|
59
|
+
if (result.valid) {
|
|
60
|
+
console.log("User is authenticated!");
|
|
61
|
+
console.log("Service:", result.serviceId);
|
|
62
|
+
console.log("Scope:", result.scope); // "user", "admin", or "service"
|
|
63
|
+
} else {
|
|
64
|
+
console.log("Invalid token:", result.error);
|
|
65
|
+
}
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
### 4. Logout
|
|
69
|
+
|
|
70
|
+
```typescript
|
|
71
|
+
await client.logout();
|
|
72
|
+
console.log("Logged out!");
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
---
|
|
76
|
+
|
|
77
|
+
## React Integration
|
|
78
|
+
|
|
79
|
+
### Setup Provider
|
|
80
|
+
|
|
81
|
+
```tsx
|
|
82
|
+
import { EasyAuthProvider } from "@easyauth/client/react";
|
|
83
|
+
|
|
84
|
+
function App() {
|
|
85
|
+
return (
|
|
86
|
+
<EasyAuthProvider
|
|
87
|
+
config={{
|
|
88
|
+
baseUrl: "https://auth.yourservice.com",
|
|
89
|
+
apiKey: "ak-xxxxx",
|
|
90
|
+
}}
|
|
91
|
+
>
|
|
92
|
+
<YourApp />
|
|
93
|
+
</EasyAuthProvider>
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
### Use Hooks
|
|
99
|
+
|
|
100
|
+
```tsx
|
|
101
|
+
import {
|
|
102
|
+
useIsAuthenticated,
|
|
103
|
+
useUser,
|
|
104
|
+
useAuthActions,
|
|
105
|
+
LoginForm
|
|
106
|
+
} from "@easyauth/client/react";
|
|
107
|
+
|
|
108
|
+
function Dashboard() {
|
|
109
|
+
const isAuthenticated = useIsAuthenticated();
|
|
110
|
+
const user = useUser();
|
|
111
|
+
const { logout } = useAuthActions();
|
|
112
|
+
|
|
113
|
+
if (!isAuthenticated) {
|
|
114
|
+
return <LoginForm onSuccess={() => console.log("Logged in!")} />;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
return (
|
|
118
|
+
<div>
|
|
119
|
+
<h1>Welcome, {user?.email}!</h1>
|
|
120
|
+
<button onClick={logout}>Logout</button>
|
|
121
|
+
</div>
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
### Protected Component
|
|
127
|
+
|
|
128
|
+
```tsx
|
|
129
|
+
import { Protected } from "@easyauth/client/react";
|
|
130
|
+
|
|
131
|
+
function App() {
|
|
132
|
+
return (
|
|
133
|
+
<Protected fallback={<p>Please login...</p>}>
|
|
134
|
+
<SecretContent />
|
|
135
|
+
</Protected>
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
---
|
|
141
|
+
|
|
142
|
+
## Next.js Integration
|
|
143
|
+
|
|
144
|
+
### Middleware Setup
|
|
145
|
+
|
|
146
|
+
Create `middleware.ts` in your project root:
|
|
147
|
+
|
|
148
|
+
```typescript
|
|
149
|
+
import { createEasyAuthMiddleware } from "@easyauth/client/next";
|
|
150
|
+
|
|
151
|
+
export const middleware = createEasyAuthMiddleware({
|
|
152
|
+
baseUrl: process.env.EASY_AUTH_URL!,
|
|
153
|
+
protectedPaths: ["/dashboard", "/profile", "/settings"],
|
|
154
|
+
authPaths: ["/login", "/signup"],
|
|
155
|
+
loginPath: "/login",
|
|
156
|
+
dashboardPath: "/dashboard",
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
export const config = {
|
|
160
|
+
matcher: ["/((?!api|_next/static|_next/image|favicon.ico).*)"],
|
|
161
|
+
};
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
### API Route Protection
|
|
165
|
+
|
|
166
|
+
```typescript
|
|
167
|
+
import { NextResponse } from "next/server";
|
|
168
|
+
import { withAuth, withScope } from "@easyauth/client/next";
|
|
169
|
+
|
|
170
|
+
// Any authenticated user
|
|
171
|
+
export const GET = withAuth(
|
|
172
|
+
{ baseUrl: process.env.EASY_AUTH_URL! },
|
|
173
|
+
async (request, { userId, scope }) => {
|
|
174
|
+
return NextResponse.json({
|
|
175
|
+
message: "Hello user!",
|
|
176
|
+
userId
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
);
|
|
180
|
+
|
|
181
|
+
// Admin only
|
|
182
|
+
export const POST = withScope(
|
|
183
|
+
{ baseUrl: process.env.EASY_AUTH_URL! },
|
|
184
|
+
["admin"], // Only admins
|
|
185
|
+
async (request, { userId }) => {
|
|
186
|
+
return NextResponse.json({
|
|
187
|
+
message: "Admin action performed"
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
);
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
### Server-Side Usage
|
|
194
|
+
|
|
195
|
+
```typescript
|
|
196
|
+
import { createServerClient } from "@easyauth/client/next";
|
|
197
|
+
|
|
198
|
+
const serverClient = createServerClient({
|
|
199
|
+
baseUrl: process.env.EASY_AUTH_URL!,
|
|
200
|
+
apiKey: process.env.EASY_AUTH_API_KEY!, // Server-only
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
// Create a user
|
|
204
|
+
const user = await serverClient.createUser({
|
|
205
|
+
email: "new@example.com",
|
|
206
|
+
password: "secure123",
|
|
207
|
+
});
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
---
|
|
211
|
+
|
|
212
|
+
## User Management
|
|
213
|
+
|
|
214
|
+
### Create User (Admin)
|
|
215
|
+
|
|
216
|
+
```typescript
|
|
217
|
+
const user = await client.createUser({
|
|
218
|
+
email: "newuser@example.com",
|
|
219
|
+
password: "securePassword123",
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
console.log("Created user:", user.userId);
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
### List Users
|
|
226
|
+
|
|
227
|
+
```typescript
|
|
228
|
+
const users = await client.listUsers();
|
|
229
|
+
|
|
230
|
+
users.forEach(user => {
|
|
231
|
+
console.log(`${user.email} (${user.userId})`);
|
|
232
|
+
});
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
### Delete User
|
|
236
|
+
|
|
237
|
+
```typescript
|
|
238
|
+
await client.deleteUser("user-xxxxx");
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
---
|
|
242
|
+
|
|
243
|
+
## API Key Management
|
|
244
|
+
|
|
245
|
+
### Create API Key
|
|
246
|
+
|
|
247
|
+
```typescript
|
|
248
|
+
const key = await client.createApiKey({
|
|
249
|
+
name: "Production Backend",
|
|
250
|
+
scope: "service",
|
|
251
|
+
tpsLimit: 1000,
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
console.log("New API Key (save this!):", key.apiKey);
|
|
255
|
+
// ⚠️ Only shown once!
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
### List API Keys
|
|
259
|
+
|
|
260
|
+
```typescript
|
|
261
|
+
const keys = await client.listApiKeys();
|
|
262
|
+
|
|
263
|
+
keys.forEach(key => {
|
|
264
|
+
console.log(`${key.name}: ${key.keyId} (${key.revoked ? "revoked" : "active"})`);
|
|
265
|
+
});
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
### Revoke API Key
|
|
269
|
+
|
|
270
|
+
```typescript
|
|
271
|
+
await client.revokeApiKey("key-xxxxx");
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
---
|
|
275
|
+
|
|
276
|
+
## Service Onboarding
|
|
277
|
+
|
|
278
|
+
For platform admins who onboard new services:
|
|
279
|
+
|
|
280
|
+
```typescript
|
|
281
|
+
import { EasyAuthClient } from "@easyauth/client";
|
|
282
|
+
|
|
283
|
+
// Use master admin key
|
|
284
|
+
const adminClient = new EasyAuthClient({
|
|
285
|
+
baseUrl: "https://auth.yourservice.com",
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
const service = await adminClient.onboardService(
|
|
289
|
+
{
|
|
290
|
+
serviceName: "my-new-service",
|
|
291
|
+
ownerEmail: "admin@example.com",
|
|
292
|
+
},
|
|
293
|
+
process.env.MASTER_ADMIN_KEY! // Master onboarding key
|
|
294
|
+
);
|
|
295
|
+
|
|
296
|
+
console.log("Service ID:", service.serviceId);
|
|
297
|
+
console.log("Admin API Key (save this!):", service.serviceAdminApiKey);
|
|
298
|
+
```
|
|
299
|
+
|
|
300
|
+
---
|
|
301
|
+
|
|
302
|
+
## Token Storage
|
|
303
|
+
|
|
304
|
+
By default, tokens are stored in `localStorage`. For custom storage:
|
|
305
|
+
|
|
306
|
+
```typescript
|
|
307
|
+
import {
|
|
308
|
+
EasyAuthClient,
|
|
309
|
+
MemoryTokenStorage
|
|
310
|
+
} from "@easyauth/client";
|
|
311
|
+
|
|
312
|
+
// Server-side or memory-only storage
|
|
313
|
+
const client = new EasyAuthClient(
|
|
314
|
+
{ baseUrl: "...", apiKey: "..." },
|
|
315
|
+
new MemoryTokenStorage()
|
|
316
|
+
);
|
|
317
|
+
```
|
|
318
|
+
|
|
319
|
+
Create custom storage:
|
|
320
|
+
|
|
321
|
+
```typescript
|
|
322
|
+
import { TokenStorage } from "@easyauth/client";
|
|
323
|
+
|
|
324
|
+
class CookieStorage implements TokenStorage {
|
|
325
|
+
getToken(): string | null {
|
|
326
|
+
// Read from cookies
|
|
327
|
+
}
|
|
328
|
+
setToken(token: string): void {
|
|
329
|
+
// Set cookie
|
|
330
|
+
}
|
|
331
|
+
removeToken(): void {
|
|
332
|
+
// Delete cookie
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
```
|
|
336
|
+
|
|
337
|
+
---
|
|
338
|
+
|
|
339
|
+
## Error Handling
|
|
340
|
+
|
|
341
|
+
```typescript
|
|
342
|
+
import { EasyAuthError } from "@easyauth/client";
|
|
343
|
+
|
|
344
|
+
try {
|
|
345
|
+
await client.login({ email, password });
|
|
346
|
+
} catch (error) {
|
|
347
|
+
if (error instanceof EasyAuthError) {
|
|
348
|
+
console.log("Error code:", error.code);
|
|
349
|
+
console.log("Status:", error.statusCode);
|
|
350
|
+
console.log("Message:", error.message);
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
```
|
|
354
|
+
|
|
355
|
+
Common error codes:
|
|
356
|
+
- `MISSING_API_KEY` - API key not configured
|
|
357
|
+
- `NETWORK_ERROR` - Connection failed
|
|
358
|
+
- `UNKNOWN_ERROR` - Server returned error
|
|
359
|
+
|
|
360
|
+
---
|
|
361
|
+
|
|
362
|
+
## Complete Example: Protected API
|
|
363
|
+
|
|
364
|
+
```typescript
|
|
365
|
+
// middleware.ts
|
|
366
|
+
import { createEasyAuthMiddleware } from "@easyauth/client/next";
|
|
367
|
+
|
|
368
|
+
export const middleware = createEasyAuthMiddleware({
|
|
369
|
+
baseUrl: process.env.EASY_AUTH_URL!,
|
|
370
|
+
protectedPaths: ["/api/protected"],
|
|
371
|
+
});
|
|
372
|
+
|
|
373
|
+
export const config = {
|
|
374
|
+
matcher: "/api/:path*",
|
|
375
|
+
};
|
|
376
|
+
|
|
377
|
+
// app/api/protected/route.ts
|
|
378
|
+
import { NextResponse } from "next/server";
|
|
379
|
+
import { withAuth } from "@easyauth/client/next";
|
|
380
|
+
|
|
381
|
+
export const GET = withAuth(
|
|
382
|
+
{ baseUrl: process.env.EASY_AUTH_URL! },
|
|
383
|
+
async (request, { userId, scope }) => {
|
|
384
|
+
// User is authenticated!
|
|
385
|
+
return NextResponse.json({
|
|
386
|
+
message: "Secret data",
|
|
387
|
+
userId,
|
|
388
|
+
scope,
|
|
389
|
+
});
|
|
390
|
+
}
|
|
391
|
+
);
|
|
392
|
+
```
|
|
393
|
+
|
|
394
|
+
---
|
|
395
|
+
|
|
396
|
+
## TypeScript Support
|
|
397
|
+
|
|
398
|
+
Full TypeScript definitions are included:
|
|
399
|
+
|
|
400
|
+
```typescript
|
|
401
|
+
import {
|
|
402
|
+
EasyAuthClient,
|
|
403
|
+
AuthToken,
|
|
404
|
+
User,
|
|
405
|
+
ApiKey,
|
|
406
|
+
ValidationResult,
|
|
407
|
+
EasyAuthConfig
|
|
408
|
+
} from "@easyauth/client";
|
|
409
|
+
```
|
|
410
|
+
|
|
411
|
+
---
|
|
412
|
+
|
|
413
|
+
## License
|
|
414
|
+
|
|
415
|
+
MIT License - see LICENSE file for details.
|
|
416
|
+
|
|
417
|
+
## Contributing
|
|
418
|
+
|
|
419
|
+
Contributions welcome! Please read our [Contributing Guide](CONTRIBUTING.md).
|
|
420
|
+
|
|
421
|
+
## Support
|
|
422
|
+
|
|
423
|
+
- 📖 [Documentation](https://docs.easyauth.io)
|
|
424
|
+
- 🐛 [Issue Tracker](https://github.com/yourorg/easy-auth/issues)
|
|
425
|
+
- 💬 [Discussions](https://github.com/yourorg/easy-auth/discussions)
|