@bagelink/auth 1.4.169 → 1.4.174
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 +254 -110
- package/dist/index.cjs +388 -159
- package/dist/index.d.cts +357 -102
- package/dist/index.d.mts +357 -102
- package/dist/index.d.ts +357 -102
- package/dist/index.mjs +389 -161
- package/package.json +1 -1
- package/src/api.ts +230 -72
- package/src/types.ts +278 -71
- package/src/useAuth.ts +212 -110
package/README.md
CHANGED
|
@@ -1,186 +1,330 @@
|
|
|
1
1
|
# @bagelink/auth
|
|
2
2
|
|
|
3
|
-
A
|
|
3
|
+
A user-centric authentication library with Vue support. Handles both person and entity accounts seamlessly.
|
|
4
4
|
|
|
5
5
|
## Features
|
|
6
6
|
|
|
7
|
-
-
|
|
8
|
-
-
|
|
9
|
-
- TypeScript support
|
|
10
|
-
-
|
|
11
|
-
-
|
|
12
|
-
-
|
|
13
|
-
- Event
|
|
7
|
+
- 🎯 **User-Centric Design** - Access user data directly, not buried in account objects
|
|
8
|
+
- 🔄 **Multi-Account Support** - Works with person, entity, and service accounts
|
|
9
|
+
- 🛡️ **Type-Safe** - Full TypeScript support
|
|
10
|
+
- ⚡ **Vue Integration** - Reactive refs with Vue 3 composables
|
|
11
|
+
- 🔐 **Complete Auth Flow** - Login, signup, password management, email verification
|
|
12
|
+
- 📡 **Session Management** - View, refresh, and revoke sessions
|
|
13
|
+
- 🎪 **Event System** - Listen to authentication state changes
|
|
14
|
+
- 🌐 **Framework Agnostic** - Core auth logic works anywhere
|
|
14
15
|
|
|
15
16
|
## Installation
|
|
16
17
|
|
|
17
18
|
```bash
|
|
19
|
+
pnpm add @bagelink/auth
|
|
20
|
+
# or
|
|
21
|
+
npm install @bagelink/auth
|
|
22
|
+
# or
|
|
18
23
|
bun add @bagelink/auth
|
|
19
24
|
```
|
|
20
25
|
|
|
21
|
-
##
|
|
26
|
+
## Quick Start
|
|
22
27
|
|
|
23
|
-
###
|
|
28
|
+
### 1. Initialize Auth
|
|
24
29
|
|
|
25
30
|
```typescript
|
|
26
|
-
import { initAuth
|
|
31
|
+
import { initAuth } from '@bagelink/auth'
|
|
32
|
+
import axios from 'axios'
|
|
27
33
|
|
|
28
|
-
// Initialize with axios instance
|
|
29
34
|
const auth = initAuth({
|
|
30
|
-
axios:
|
|
35
|
+
axios: axios,
|
|
31
36
|
baseURL: 'https://api.example.com'
|
|
32
37
|
})
|
|
33
38
|
|
|
34
|
-
//
|
|
39
|
+
// Listen to auth events
|
|
35
40
|
auth.on(AuthState.LOGIN, () => {
|
|
36
41
|
console.log('User logged in!')
|
|
37
|
-
// Redirect to dashboard, update UI, etc.
|
|
38
42
|
})
|
|
43
|
+
```
|
|
39
44
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
45
|
+
### 2. Use in Vue Components
|
|
46
|
+
|
|
47
|
+
```vue
|
|
48
|
+
<script setup lang="ts">
|
|
49
|
+
import { useAuth } from '@bagelink/auth'
|
|
50
|
+
|
|
51
|
+
const {
|
|
52
|
+
user, // Primary state - use this!
|
|
53
|
+
getIsLoggedIn,
|
|
54
|
+
login,
|
|
55
|
+
logout
|
|
56
|
+
} = useAuth()
|
|
57
|
+
|
|
58
|
+
const handleLogin = async () => {
|
|
59
|
+
await login({
|
|
60
|
+
email: 'user@example.com',
|
|
61
|
+
password: 'password'
|
|
62
|
+
})
|
|
63
|
+
}
|
|
64
|
+
</script>
|
|
65
|
+
|
|
66
|
+
<template>
|
|
67
|
+
<div v-if="user">
|
|
68
|
+
<h1>Welcome, {{ user.name }}!</h1>
|
|
69
|
+
<p>Email: {{ user.email }}</p>
|
|
70
|
+
<button @click="logout">Logout</button>
|
|
71
|
+
</div>
|
|
72
|
+
<div v-else>
|
|
73
|
+
<button @click="handleLogin">Login</button>
|
|
74
|
+
</div>
|
|
75
|
+
</template>
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
## Core Concepts
|
|
79
|
+
|
|
80
|
+
### The `user` Object
|
|
81
|
+
|
|
82
|
+
The `user` is a **computed ref** that provides a unified interface for both person and entity accounts:
|
|
83
|
+
|
|
84
|
+
```typescript
|
|
85
|
+
const { user } = useAuth()
|
|
86
|
+
|
|
87
|
+
// Available for all account types
|
|
88
|
+
user.value?.id // Person ID or Entity ID
|
|
89
|
+
user.value?.name // Display name
|
|
90
|
+
user.value?.email // Email address
|
|
91
|
+
user.value?.type // 'person', 'entity', or 'service'
|
|
92
|
+
user.value?.isActive // Is account active
|
|
93
|
+
user.value?.isVerified // Is account verified
|
|
94
|
+
|
|
95
|
+
// Person-specific
|
|
96
|
+
user.value?.roles // User roles (e.g., ['admin', 'user'])
|
|
97
|
+
|
|
98
|
+
// Entity-specific
|
|
99
|
+
user.value?.entityType // Entity type (e.g., 'company', 'organization')
|
|
100
|
+
user.value?.metadata // Additional entity metadata
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
### Account Info (Advanced)
|
|
104
|
+
|
|
105
|
+
For authentication-specific data, use `accountInfo`:
|
|
106
|
+
|
|
107
|
+
```typescript
|
|
108
|
+
const { accountInfo } = useAuth()
|
|
109
|
+
|
|
110
|
+
accountInfo.value?.authentication_methods // Auth methods
|
|
111
|
+
accountInfo.value?.last_login // Last login timestamp
|
|
112
|
+
accountInfo.value?.person // Raw person data
|
|
113
|
+
accountInfo.value?.entity // Raw entity data
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
## API Reference
|
|
117
|
+
|
|
118
|
+
### State
|
|
119
|
+
|
|
120
|
+
```typescript
|
|
121
|
+
const {
|
|
122
|
+
user, // Computed<User | null> - Primary state
|
|
123
|
+
accountInfo, // Ref<AccountInfo | null> - Full account data
|
|
124
|
+
} = useAuth()
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
### Getters
|
|
128
|
+
|
|
129
|
+
```typescript
|
|
130
|
+
const {
|
|
131
|
+
getFullName, // () => string
|
|
132
|
+
getIsLoggedIn, // () => boolean
|
|
133
|
+
getEmail, // () => string
|
|
134
|
+
getRoles, // () => string[]
|
|
135
|
+
getAccountType, // () => 'person' | 'entity' | 'service'
|
|
136
|
+
isPersonAccount, // () => boolean
|
|
137
|
+
isEntityAccount, // () => boolean
|
|
138
|
+
} = useAuth()
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
### Authentication
|
|
142
|
+
|
|
143
|
+
```typescript
|
|
144
|
+
// Login
|
|
145
|
+
await login({ email: 'user@example.com', password: 'password' })
|
|
146
|
+
|
|
147
|
+
// Signup
|
|
148
|
+
await signup({
|
|
149
|
+
email: 'user@example.com',
|
|
150
|
+
first_name: 'John',
|
|
151
|
+
last_name: 'Doe',
|
|
152
|
+
password: 'password',
|
|
153
|
+
confirmPassword: 'password'
|
|
43
154
|
})
|
|
155
|
+
|
|
156
|
+
// Logout
|
|
157
|
+
await logout()
|
|
158
|
+
|
|
159
|
+
// Check auth status
|
|
160
|
+
const isAuthenticated = await checkAuth()
|
|
161
|
+
|
|
162
|
+
// Refresh session
|
|
163
|
+
await refreshSession()
|
|
44
164
|
```
|
|
45
165
|
|
|
46
|
-
###
|
|
166
|
+
### Password Management
|
|
47
167
|
|
|
48
168
|
```typescript
|
|
49
|
-
//
|
|
50
|
-
|
|
51
|
-
|
|
169
|
+
// Change password (requires current password)
|
|
170
|
+
await changePassword({
|
|
171
|
+
current_password: 'oldPassword',
|
|
172
|
+
new_password: 'newPassword',
|
|
173
|
+
confirmNewPassword: 'newPassword'
|
|
174
|
+
})
|
|
52
175
|
|
|
53
|
-
|
|
176
|
+
// Forgot password
|
|
177
|
+
await forgotPassword('user@example.com')
|
|
54
178
|
|
|
55
|
-
//
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
179
|
+
// Verify reset token
|
|
180
|
+
await verifyResetToken(token)
|
|
181
|
+
|
|
182
|
+
// Reset password with token
|
|
183
|
+
await resetPassword(token, 'newPassword')
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
### Email Verification
|
|
187
|
+
|
|
188
|
+
```typescript
|
|
189
|
+
// Send verification email
|
|
190
|
+
await sendVerification('user@example.com')
|
|
191
|
+
|
|
192
|
+
// Verify email with token
|
|
193
|
+
await verifyEmail(token)
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
### Profile Management
|
|
197
|
+
|
|
198
|
+
```typescript
|
|
199
|
+
// Update profile
|
|
200
|
+
await updateProfile({
|
|
201
|
+
first_name: 'Jane',
|
|
202
|
+
last_name: 'Smith',
|
|
203
|
+
email: 'jane@example.com'
|
|
59
204
|
})
|
|
60
205
|
|
|
61
|
-
//
|
|
62
|
-
|
|
206
|
+
// Delete current user account
|
|
207
|
+
await deleteCurrentUser()
|
|
208
|
+
```
|
|
63
209
|
|
|
64
|
-
|
|
65
|
-
import { useAuth } from '@bagelink/auth'
|
|
210
|
+
### Session Management
|
|
66
211
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
212
|
+
```typescript
|
|
213
|
+
// Get active sessions
|
|
214
|
+
const { data } = await getSessions()
|
|
215
|
+
const sessions = data.sessions
|
|
216
|
+
|
|
217
|
+
// Revoke specific session
|
|
218
|
+
await revokeSession(sessionToken)
|
|
219
|
+
|
|
220
|
+
// Revoke all sessions
|
|
221
|
+
await revokeAllSessions()
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
### Admin Actions
|
|
225
|
+
|
|
226
|
+
```typescript
|
|
227
|
+
// Activate/deactivate accounts
|
|
228
|
+
await activateAccount(accountId)
|
|
229
|
+
await deactivateAccount(accountId)
|
|
230
|
+
|
|
231
|
+
// Delete account
|
|
232
|
+
await deleteAccount(accountId)
|
|
86
233
|
```
|
|
87
234
|
|
|
88
|
-
|
|
235
|
+
## Event System
|
|
89
236
|
|
|
90
|
-
|
|
237
|
+
Listen to authentication state changes:
|
|
91
238
|
|
|
92
239
|
```typescript
|
|
93
240
|
import { initAuth, AuthState } from '@bagelink/auth'
|
|
94
241
|
|
|
95
|
-
const auth = initAuth({
|
|
96
|
-
axios: axiosInstance,
|
|
97
|
-
baseURL: 'https://api.example.com'
|
|
98
|
-
})
|
|
242
|
+
const auth = initAuth({ axios })
|
|
99
243
|
|
|
100
|
-
//
|
|
244
|
+
// Login
|
|
101
245
|
auth.on(AuthState.LOGIN, () => {
|
|
102
|
-
|
|
246
|
+
console.log('User logged in')
|
|
103
247
|
})
|
|
104
248
|
|
|
105
|
-
//
|
|
249
|
+
// Logout
|
|
106
250
|
auth.on(AuthState.LOGOUT, () => {
|
|
107
|
-
|
|
251
|
+
console.log('User logged out')
|
|
108
252
|
})
|
|
109
253
|
|
|
110
|
-
//
|
|
254
|
+
// Signup
|
|
111
255
|
auth.on(AuthState.SIGNUP, () => {
|
|
112
|
-
|
|
256
|
+
console.log('User signed up')
|
|
257
|
+
})
|
|
258
|
+
|
|
259
|
+
// Password changed
|
|
260
|
+
auth.on(AuthState.PASSWORD_CHANGE, () => {
|
|
261
|
+
console.log('Password changed')
|
|
113
262
|
})
|
|
114
263
|
|
|
115
|
-
//
|
|
264
|
+
// Password reset
|
|
116
265
|
auth.on(AuthState.PASSWORD_RESET, () => {
|
|
117
|
-
console.log('Password
|
|
266
|
+
console.log('Password reset')
|
|
118
267
|
})
|
|
119
268
|
|
|
120
|
-
//
|
|
269
|
+
// Profile updated
|
|
121
270
|
auth.on(AuthState.PROFILE_UPDATE, () => {
|
|
122
|
-
console.log('Profile updated
|
|
271
|
+
console.log('Profile updated')
|
|
123
272
|
})
|
|
124
273
|
|
|
125
|
-
//
|
|
274
|
+
// Auth check completed
|
|
126
275
|
auth.on(AuthState.AUTH_CHECK, () => {
|
|
127
|
-
console.log('
|
|
276
|
+
console.log('Auth verified')
|
|
128
277
|
})
|
|
129
|
-
```
|
|
130
|
-
|
|
131
|
-
#### Event Methods
|
|
132
278
|
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
#### Available Events
|
|
279
|
+
// Email verified
|
|
280
|
+
auth.on(AuthState.EMAIL_VERIFIED, () => {
|
|
281
|
+
console.log('Email verified')
|
|
282
|
+
})
|
|
138
283
|
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
- `AuthState.AUTH_CHECK`: Fired when user authentication is verified
|
|
284
|
+
// Session refreshed
|
|
285
|
+
auth.on(AuthState.SESSION_REFRESH, () => {
|
|
286
|
+
console.log('Session refreshed')
|
|
287
|
+
})
|
|
288
|
+
```
|
|
145
289
|
|
|
146
|
-
###
|
|
290
|
+
### Event Methods
|
|
147
291
|
|
|
148
292
|
```typescript
|
|
149
|
-
|
|
293
|
+
// Add listener
|
|
294
|
+
auth.on(AuthState.LOGIN, handler)
|
|
150
295
|
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
baseURL: 'https://api.example.com'
|
|
154
|
-
})
|
|
155
|
-
|
|
156
|
-
const { login, currentUser, getIsLoggedIn } = auth.useAuth()
|
|
157
|
-
```
|
|
296
|
+
// Remove listener
|
|
297
|
+
auth.off(AuthState.LOGIN, handler)
|
|
158
298
|
|
|
159
|
-
|
|
299
|
+
// Remove all listeners for an event
|
|
300
|
+
auth.removeAllListeners(AuthState.LOGIN)
|
|
160
301
|
|
|
161
|
-
|
|
302
|
+
// Remove all listeners
|
|
303
|
+
auth.removeAllListeners()
|
|
304
|
+
```
|
|
162
305
|
|
|
163
|
-
|
|
164
|
-
- `passwordForm`: Password update form state
|
|
306
|
+
## TypeScript Support
|
|
165
307
|
|
|
166
|
-
|
|
308
|
+
Full TypeScript support with exported types:
|
|
167
309
|
|
|
168
|
-
|
|
169
|
-
|
|
310
|
+
```typescript
|
|
311
|
+
import type {
|
|
312
|
+
User,
|
|
313
|
+
AccountInfo,
|
|
314
|
+
PersonInfo,
|
|
315
|
+
EntityInfo,
|
|
316
|
+
RegisterRequest,
|
|
317
|
+
UpdateAccountRequest,
|
|
318
|
+
AuthenticationResponse,
|
|
319
|
+
SessionInfo,
|
|
320
|
+
// ... and more
|
|
321
|
+
} from '@bagelink/auth'
|
|
322
|
+
```
|
|
170
323
|
|
|
171
|
-
|
|
324
|
+
## Migration
|
|
172
325
|
|
|
173
|
-
|
|
174
|
-
- `logout()`: Logout current user
|
|
175
|
-
- `checkAuth()`: Check authentication status
|
|
176
|
-
- `signup(user)`: Register new user
|
|
177
|
-
- `recoverPassword(email)`: Request password recovery
|
|
178
|
-
- `resetPassword(form)`: Reset password
|
|
179
|
-
- `updatePassword()`: Update current user's password
|
|
180
|
-
- `updateProfile(user)`: Update user profile
|
|
181
|
-
- `toggleUserStatus(userId, isActive)`: Toggle user active status
|
|
182
|
-
- `deleteUser(userId)`: Delete user
|
|
326
|
+
Upgrading from an older version? See [MIGRATION.md](./MIGRATION.md) for detailed migration instructions.
|
|
183
327
|
|
|
184
328
|
## License
|
|
185
329
|
|
|
186
|
-
MIT
|
|
330
|
+
MIT
|