@meistrari/auth-core 0.1.1 → 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,444 @@
1
+ # Identity Provider SDK - Core
2
+
3
+ A TypeScript/JavaScript SDK for interacting with the Auth API service.
4
+
5
+ ## Features
6
+
7
+ - **Multiple Authentication Methods**
8
+ - Social providers (Google, Microsoft)
9
+ - SAML-based SSO
10
+ - Email and password
11
+ - **Organization Management**
12
+ - Multi-tenant support
13
+ - Role-based access control (RBAC)
14
+ - Team management
15
+ - Member invitations
16
+ - **JWT Token Validation**
17
+
18
+ ## Installation
19
+
20
+ ```bash
21
+ npm install @meistrari/auth-core
22
+ ```
23
+
24
+ ## Quick Start
25
+
26
+ ```typescript
27
+ import { AuthClient } from '@meistrari/auth-core'
28
+
29
+ // Initialize the client
30
+ const authClient = new AuthClient('https://auth.example.com')
31
+
32
+ // Sign in with email and password
33
+ await authClient.session.signInWithEmailAndPassword({
34
+ email: 'user@example.com',
35
+ password: 'SecurePassword123!'
36
+ })
37
+
38
+ // List user's organizations
39
+ const organizations = await authClient.organization.listOrganizations()
40
+ console.log('Organizations:', organizations)
41
+ ```
42
+
43
+ ## API Reference
44
+
45
+ ### AuthClient
46
+
47
+ The main entry point for the SDK. Provides access to session and organization services.
48
+
49
+ #### Constructor
50
+
51
+ ```typescript
52
+ new AuthClient(apiUrl: string, headers?: Record<string, string>)
53
+ ```
54
+
55
+ **Parameters:**
56
+ - `apiUrl` - The base URL of the Auth API
57
+ - `headers` (optional) - Custom headers to include in all requests
58
+
59
+ **Example:**
60
+ ```typescript
61
+ const authClient = new AuthClient('https://auth.example.com', {
62
+ 'X-Custom-Header': 'value'
63
+ })
64
+ ```
65
+
66
+ ### Session Management
67
+
68
+ Access via `authClient.session`
69
+
70
+ #### signInWithEmailAndPassword
71
+
72
+ Authenticates a user with email and password credentials.
73
+
74
+ ```typescript
75
+ await authClient.session.signInWithEmailAndPassword({
76
+ email: 'user@example.com',
77
+ password: 'password123'
78
+ })
79
+ ```
80
+
81
+ #### signInWithSocialProvider
82
+
83
+ Initiates social authentication flow with Google or Microsoft.
84
+
85
+ ```typescript
86
+ await authClient.session.signInWithSocialProvider({
87
+ provider: 'google', // or 'microsoft'
88
+ callbackURL: 'https://app.example.com/callback',
89
+ errorCallbackURL: 'https://app.example.com/error' // optional
90
+ })
91
+ ```
92
+
93
+ The error callback URL receives an `error` query parameter with the error status text.
94
+
95
+ #### signInWithSaml
96
+
97
+ Initiates SAML-based Single Sign-On authentication flow.
98
+
99
+ ```typescript
100
+ await authClient.session.signInWithSaml({
101
+ email: 'user@company.com',
102
+ callbackURL: 'https://app.example.com/callback',
103
+ errorCallbackURL: 'https://app.example.com/error' // optional
104
+ })
105
+ ```
106
+
107
+ The error callback URL receives an `error` query parameter with the error status text.
108
+
109
+ #### signOut
110
+
111
+ Signs out the currently authenticated user.
112
+
113
+ ```typescript
114
+ // Simple sign out
115
+ await authClient.session.signOut()
116
+
117
+ // Sign out with callback
118
+ await authClient.session.signOut(() => {
119
+ console.log('User signed out')
120
+ window.location.href = '/login'
121
+ })
122
+ ```
123
+
124
+ #### requestPasswordReset
125
+
126
+ Initiates a password reset request.
127
+
128
+ ```typescript
129
+ await authClient.session.requestPasswordReset(
130
+ 'user@example.com',
131
+ 'https://app.example.com/reset-password'
132
+ )
133
+ ```
134
+
135
+ #### resetPassword
136
+
137
+ Completes the password reset process.
138
+
139
+ ```typescript
140
+ await authClient.session.resetPassword(
141
+ 'reset-token-from-email',
142
+ 'NewSecurePassword123!'
143
+ )
144
+ ```
145
+
146
+ ### Organization Management
147
+
148
+ Access via `authClient.organization`
149
+
150
+ #### listOrganizations
151
+
152
+ Lists all organizations accessible to the current user.
153
+
154
+ ```typescript
155
+ const organizations = await authClient.organization.listOrganizations()
156
+ ```
157
+
158
+ #### getOrganization
159
+
160
+ Retrieves a single organization with optional related data.
161
+
162
+ ```typescript
163
+ const organization = await authClient.organization.getOrganization('org-123', {
164
+ includeMembers: true,
165
+ includeInvitations: true,
166
+ includeTeams: true
167
+ })
168
+ ```
169
+
170
+ #### setActiveOrganization
171
+
172
+ Sets the active organization for the current user session.
173
+
174
+ ```typescript
175
+ await authClient.organization.setActiveOrganization('org-123')
176
+ ```
177
+
178
+ #### updateOrganization
179
+
180
+ Updates an organization's details.
181
+
182
+ ```typescript
183
+ const updated = await authClient.organization.updateOrganization({
184
+ name: 'New Organization Name',
185
+ logo: 'https://example.com/logo.png',
186
+ settings: {
187
+ disableStorage: true,
188
+ dataRetentionHours: 24
189
+ }
190
+ })
191
+ ```
192
+
193
+ ### Member Management
194
+
195
+ #### listMembers
196
+
197
+ Lists members of the active organization.
198
+
199
+ ```typescript
200
+ const members = await authClient.organization.listMembers({
201
+ limit: 10,
202
+ offset: 0
203
+ })
204
+ ```
205
+
206
+ #### getActiveMember
207
+
208
+ Gets the currently active organization member for the authenticated user.
209
+
210
+ ```typescript
211
+ const activeMember = await authClient.organization.getActiveMember()
212
+ console.log(`Role: ${activeMember.role}`)
213
+ ```
214
+
215
+ #### inviteUserToOrganization
216
+
217
+ Invites a user to join the active organization.
218
+
219
+ ```typescript
220
+ import { Roles } from '@meistrari/auth-core'
221
+
222
+ await authClient.organization.inviteUserToOrganization({
223
+ userEmail: 'newuser@example.com',
224
+ role: Roles.ORG_MEMBER,
225
+ teamId: 'team-123' // optional
226
+ })
227
+ ```
228
+
229
+ #### acceptInvitation
230
+
231
+ Accepts an organization invitation.
232
+
233
+ ```typescript
234
+ await authClient.organization.acceptInvitation('invitation-123')
235
+ ```
236
+
237
+ #### cancelInvitation
238
+
239
+ Cancels a pending invitation.
240
+
241
+ ```typescript
242
+ await authClient.organization.cancelInvitation('invitation-123')
243
+ ```
244
+
245
+ #### removeUserFromOrganization
246
+
247
+ Removes a user from the active organization.
248
+
249
+ ```typescript
250
+ // Remove by member ID
251
+ await authClient.organization.removeUserFromOrganization({
252
+ memberId: 'member-123'
253
+ })
254
+
255
+ // Remove by email
256
+ await authClient.organization.removeUserFromOrganization({
257
+ userEmail: 'user@example.com'
258
+ })
259
+ ```
260
+
261
+ #### updateMemberRole
262
+
263
+ Updates the role of an organization member.
264
+
265
+ ```typescript
266
+ import { Roles } from '@meistrari/auth-core'
267
+
268
+ await authClient.organization.updateMemberRole({
269
+ memberId: 'member-123',
270
+ role: Roles.ORG_ADMIN
271
+ })
272
+ ```
273
+
274
+ ### Team Management
275
+
276
+ #### listTeams
277
+
278
+ Lists all teams in the active organization.
279
+
280
+ ```typescript
281
+ const teams = await authClient.organization.listTeams()
282
+ ```
283
+
284
+ #### createTeam
285
+
286
+ Creates a new team.
287
+
288
+ ```typescript
289
+ const team = await authClient.organization.createTeam({
290
+ name: 'Engineering Team'
291
+ })
292
+ ```
293
+
294
+ #### updateTeam
295
+
296
+ Updates a team's details.
297
+
298
+ ```typescript
299
+ const updated = await authClient.organization.updateTeam('team-123', {
300
+ name: 'Updated Team Name'
301
+ })
302
+ ```
303
+
304
+ #### deleteTeam
305
+
306
+ Deletes a team.
307
+
308
+ ```typescript
309
+ await authClient.organization.deleteTeam('team-123')
310
+ ```
311
+
312
+ #### listTeamMembers
313
+
314
+ Lists all members of a specific team.
315
+
316
+ ```typescript
317
+ const members = await authClient.organization.listTeamMembers('team-123')
318
+ ```
319
+
320
+ #### addTeamMember
321
+
322
+ Adds a user to a team.
323
+
324
+ ```typescript
325
+ await authClient.organization.addTeamMember('team-123', 'user-456')
326
+ ```
327
+
328
+ #### removeTeamMember
329
+
330
+ Removes a user from a team.
331
+
332
+ ```typescript
333
+ await authClient.organization.removeTeamMember('team-123', 'user-456')
334
+ ```
335
+
336
+ ### Token Validation
337
+
338
+ Standalone utility functions for JWT token validation.
339
+
340
+ #### isTokenExpired
341
+
342
+ Checks if a JWT token has expired.
343
+
344
+ ```typescript
345
+ import { isTokenExpired } from '@meistrari/auth-core'
346
+
347
+ const token = 'eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9...'
348
+ if (isTokenExpired(token)) {
349
+ console.log('Token has expired')
350
+ }
351
+ ```
352
+
353
+ #### validateToken
354
+
355
+ Validates a JWT token by checking expiration and verifying signature.
356
+
357
+ ```typescript
358
+ import { validateToken } from '@meistrari/auth-core'
359
+
360
+ const token = 'eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9...'
361
+ const isValid = await validateToken(token, 'https://auth.example.com')
362
+
363
+ if (isValid) {
364
+ console.log('Token is valid')
365
+ }
366
+ ```
367
+
368
+ ## Roles and Permissions
369
+
370
+ The SDK provides predefined organization roles with specific permissions:
371
+
372
+ ```typescript
373
+ import { Roles } from '@meistrari/auth-core'
374
+
375
+ // Available roles
376
+ Roles.ORG_ADMIN // Full access: org updates, team/member management, invitations
377
+ Roles.ORG_MEMBER // Basic member access
378
+ Roles.ORG_REVIEWER // Read-only reviewer access
379
+ ```
380
+
381
+ ## Error Handling
382
+
383
+ The SDK provides typed error classes for better error handling:
384
+
385
+ ### BaseError
386
+
387
+ Base class for all SDK errors with a `code` property.
388
+
389
+ ```typescript
390
+ import { BaseError } from '@meistrari/auth-core'
391
+
392
+ try {
393
+ // SDK operation
394
+ } catch (error) {
395
+ if (error instanceof BaseError) {
396
+ console.log(`Error code: ${error.code}`)
397
+ console.log(`Error message: ${error.message}`)
398
+ }
399
+ }
400
+ ```
401
+
402
+ ### Specific Error Classes
403
+
404
+ - **InvalidSocialProvider** - Invalid social provider specified
405
+ - **InvalidCallbackURL** - Malformed callback URL
406
+ - **EmailRequired** - Email parameter required but not provided
407
+ - **APIError** (BetterFetchError) - API request failed
408
+
409
+ ```typescript
410
+ import {
411
+ InvalidSocialProvider,
412
+ InvalidCallbackURL,
413
+ EmailRequired,
414
+ APIError
415
+ } from '@meistrari/auth-core'
416
+
417
+ try {
418
+ await authClient.session.signInWithSocialProvider({
419
+ provider: 'invalid' as any,
420
+ callbackURL: 'https://app.example.com/callback'
421
+ })
422
+ } catch (error) {
423
+ if (error instanceof InvalidSocialProvider) {
424
+ console.log('Invalid provider specified')
425
+ }
426
+ }
427
+ ```
428
+
429
+ ## Type Definitions
430
+
431
+ The SDK exports TypeScript types for all data structures:
432
+
433
+ ```typescript
434
+ import type {
435
+ User,
436
+ Session,
437
+ Organization,
438
+ Member,
439
+ Invitation,
440
+ Team,
441
+ TeamMember,
442
+ Role
443
+ } from '@meistrari/auth-core'
444
+ ```