@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/src/types.ts CHANGED
@@ -1,39 +1,20 @@
1
1
  /* eslint-disable no-unused-vars */
2
2
  import type { AxiosResponse } from 'axios'
3
3
 
4
- export interface User {
5
- id: string
6
- email: string
7
- first_name?: string
8
- last_name?: string
9
- is_superuser?: boolean
10
- is_active?: boolean
11
- }
12
-
13
- export interface UserRegister {
14
- email: string
15
- password: string
16
- first_name: string
17
- last_name: string
18
- }
19
-
20
- export interface NewUser extends UserRegister {
21
- confirmPassword: string
22
- }
23
-
24
- export interface UpdatePasswordForm {
25
- current_password: string
26
- new_password: string
27
- confirmNewPassword: string
28
- }
4
+ // ============================================
5
+ // Auth State & Events
6
+ // ============================================
29
7
 
30
8
  export enum AuthState {
31
9
  LOGIN = 'login',
32
10
  LOGOUT = 'logout',
33
11
  SIGNUP = 'signup',
34
12
  PASSWORD_RESET = 'password_reset',
13
+ PASSWORD_CHANGE = 'password_change',
35
14
  PROFILE_UPDATE = 'profile_update',
36
15
  AUTH_CHECK = 'auth_check',
16
+ EMAIL_VERIFIED = 'email_verified',
17
+ SESSION_REFRESH = 'session_refresh',
37
18
  }
38
19
 
39
20
  export type AuthEventHandler = () => void
@@ -42,75 +23,301 @@ export interface AuthEventMap {
42
23
  [AuthState.LOGOUT]: AuthEventHandler
43
24
  [AuthState.SIGNUP]: AuthEventHandler
44
25
  [AuthState.PASSWORD_RESET]: AuthEventHandler
26
+ [AuthState.PASSWORD_CHANGE]: AuthEventHandler
45
27
  [AuthState.PROFILE_UPDATE]: AuthEventHandler
46
28
  [AuthState.AUTH_CHECK]: AuthEventHandler
29
+ [AuthState.EMAIL_VERIFIED]: AuthEventHandler
30
+ [AuthState.SESSION_REFRESH]: AuthEventHandler
47
31
  }
48
32
 
49
- // API Response Types
50
- export interface Token {
51
- access_token: string
33
+ // ============================================
34
+ // Core Types
35
+ // ============================================
36
+
37
+ export type AuthenticationAccountType = 'person' | 'entity' | 'service'
38
+
39
+ export type AuthenticationMethodType =
40
+ | 'password'
41
+ | 'email_token'
42
+ | 'sso'
43
+ | 'otp'
44
+
45
+ export interface AuthenticationAccount {
46
+ created_at?: string
47
+ updated_at?: string
48
+ account_type?: AuthenticationAccountType
49
+ person_id?: string
50
+ entity_id?: string
51
+ display_name?: string
52
+ is_active?: boolean
53
+ is_verified?: boolean
54
+ account_metadata?: string
55
+ last_login_at?: string
56
+ failed_login_attempts?: number
57
+ locked_until?: string
58
+ id: string
52
59
  }
53
60
 
54
- export interface PasswordRecovery {
55
- email: string
61
+ export interface PersonInfo {
62
+ id: string
63
+ name: string
64
+ email?: string
65
+ roles: string[]
56
66
  }
57
67
 
58
- export interface NewPassword {
59
- new_password: string
68
+ export interface AuthMethodInfo {
69
+ id: string
70
+ type: string
71
+ identifier?: string
72
+ is_verified: boolean
73
+ last_used?: string
74
+ use_count: number
60
75
  }
61
76
 
62
- export interface UserUpdate {
63
- email?: string
64
- is_active?: boolean
65
- is_superuser?: boolean
66
- first_name?: string
67
- last_name?: string
77
+ export interface AccountInfo {
78
+ id: string
79
+ account_type: string
80
+ display_name: string
81
+ is_active: boolean
82
+ is_verified: boolean
83
+ last_login?: string
84
+ authentication_methods: AuthMethodInfo[]
85
+ person?: PersonInfo
86
+ entity?: EntityInfo
87
+ }
88
+
89
+ export interface EntityInfo {
90
+ id: string
91
+ name: string
92
+ type?: string
93
+ metadata?: Record<string, any>
68
94
  }
69
95
 
70
- export interface UserUpdateMe {
96
+ export interface SessionInfo {
97
+ id: string
98
+ created_at: string
99
+ expires_at: string
100
+ ip_address?: string
101
+ user_agent?: string
102
+ is_current?: boolean
103
+ }
104
+
105
+ // ============================================
106
+ // Unified User Type (Person or Entity)
107
+ // ============================================
108
+
109
+ /**
110
+ * Unified user representation that works for both person and entity accounts
111
+ * This is the primary interface for accessing user data in the application
112
+ */
113
+ export interface User {
114
+ /** Unique identifier (person_id or entity_id) */
115
+ id: string
116
+ /** Account ID */
117
+ accountId: string
118
+ /** Display name */
119
+ name: string
120
+ /** Email address (from person or authentication methods) */
71
121
  email?: string
122
+ /** Account type: 'person', 'entity', or 'service' */
123
+ type: AuthenticationAccountType
124
+ /** User roles (only for person accounts) */
125
+ roles?: string[]
126
+ /** Is the account active */
127
+ isActive: boolean
128
+ /** Is the account verified */
129
+ isVerified: boolean
130
+ /** Last login timestamp */
131
+ lastLogin?: string
132
+ /** Entity-specific info (only for entity accounts) */
133
+ entityType?: string
134
+ /** Additional metadata */
135
+ metadata?: Record<string, any>
136
+ }
137
+
138
+ // ============================================
139
+ // Request Types
140
+ // ============================================
141
+
142
+ export interface RegisterRequest {
143
+ email: string
144
+ first_name: string
145
+ last_name: string
146
+ phone_number?: string
147
+ password?: string
148
+ }
149
+
150
+ export interface UpdateAccountRequest {
72
151
  first_name?: string
73
152
  last_name?: string
153
+ email?: string
154
+ phone_number?: string
155
+ }
156
+
157
+ export interface PasswordLoginRequest {
158
+ email: string
159
+ password: string
74
160
  }
75
161
 
76
- export interface UpdatePassword {
162
+ export interface ChangePasswordRequest {
77
163
  current_password: string
78
164
  new_password: string
79
165
  }
80
166
 
81
- export interface UserCreate {
167
+ export interface ForgotPasswordRequest {
82
168
  email: string
83
- password: string
84
- first_name?: string
85
- last_name?: string
86
- is_active?: boolean
87
- is_superuser?: boolean
88
169
  }
89
170
 
90
- export interface SanitizedUserOut {
91
- email: string
92
- is_active?: boolean
93
- is_superuser?: boolean
94
- first_name?: string
95
- last_name?: string
96
- id: string
171
+ export interface ResetPasswordRequest {
172
+ token: string
173
+ new_password: string
174
+ }
175
+
176
+ export interface SendVerificationRequest {
177
+ email?: string
97
178
  }
98
179
 
99
- export interface SanitizedUserList {
100
- data: SanitizedUserOut[]
101
- count: number
102
- }
103
-
104
- // API Response Types
105
- export type LoginResponse = AxiosResponse<Token>
106
- export type PasswordRecoveryResponse = AxiosResponse
107
- export type ResetPasswordResponse = AxiosResponse
108
- export type GetUserResponse = AxiosResponse<SanitizedUserOut>
109
- export type UpdateUserResponse = AxiosResponse<SanitizedUserOut>
110
- export type DeleteUserResponse = AxiosResponse
111
- export type GetUsersResponse = AxiosResponse<SanitizedUserList>
112
- export type CreateUserResponse = AxiosResponse<SanitizedUserOut>
113
- export type GetMeResponse = AxiosResponse<SanitizedUserOut>
114
- export type UpdateMeResponse = AxiosResponse<SanitizedUserOut>
115
- export type UpdatePasswordResponse = AxiosResponse
116
- export type SignupResponse = AxiosResponse<SanitizedUserOut>
180
+ export interface VerifyEmailRequest {
181
+ token: string
182
+ }
183
+
184
+ // Client-side helper types
185
+ export interface NewUser extends RegisterRequest {
186
+ confirmPassword: string
187
+ }
188
+
189
+ export interface UpdatePasswordForm extends ChangePasswordRequest {
190
+ confirmNewPassword: string
191
+ }
192
+
193
+ // ============================================
194
+ // Response Types
195
+ // ============================================
196
+
197
+ export interface MessageResponse {
198
+ message: string
199
+ }
200
+
201
+ export interface AuthStatusResponse {
202
+ status: string
203
+ methods: string[]
204
+ }
205
+
206
+ export interface AvailableMethodsResponse {
207
+ available_methods: { [key: string]: any }[]
208
+ }
209
+
210
+ export interface OTPMetadata {
211
+ nonce: string
212
+ verification_hash: string
213
+ timestamp: number
214
+ expires_in_minutes: number
215
+ autofill: { [key: string]: any }
216
+ }
217
+
218
+ export interface SSOMetadata {
219
+ provider: string
220
+ sso_user_info: { [key: string]: any }
221
+ can_create_account?: boolean
222
+ }
223
+
224
+ export interface AuthenticationResponse {
225
+ success: boolean
226
+ account_id?: string
227
+ session_token?: string
228
+ requires_verification?: boolean
229
+ verification_method?: AuthenticationMethodType
230
+ message?: string
231
+ metadata?: OTPMetadata | SSOMetadata | { [key: string]: any }
232
+ }
233
+
234
+ export interface SessionListResponse {
235
+ account_id: string
236
+ sessions: SessionInfo[]
237
+ }
238
+
239
+ // ============================================
240
+ // Axios Response Types
241
+ // ============================================
242
+
243
+ export type LoginResponse = AxiosResponse<AuthenticationResponse>
244
+ export type RegisterResponse = AxiosResponse<AuthenticationResponse>
245
+ export type LogoutResponse = AxiosResponse<MessageResponse>
246
+ export type GetMeResponse = AxiosResponse<AccountInfo>
247
+ export type UpdateMeResponse = AxiosResponse<AccountInfo>
248
+ export type DeleteMeResponse = AxiosResponse<MessageResponse>
249
+ export type GetAccountResponse = AxiosResponse<AccountInfo>
250
+ export type UpdateAccountResponse = AxiosResponse<AccountInfo>
251
+ export type DeleteAccountResponse = AxiosResponse<MessageResponse>
252
+ export type ActivateAccountResponse = AxiosResponse<AccountInfo>
253
+ export type DeactivateAccountResponse = AxiosResponse<AccountInfo>
254
+ export type ChangePasswordResponse = AxiosResponse<MessageResponse>
255
+ export type ForgotPasswordResponse = AxiosResponse<MessageResponse>
256
+ export type ResetPasswordResponse = AxiosResponse<MessageResponse>
257
+ export type VerifyResetTokenResponse = AxiosResponse<MessageResponse>
258
+ export type SendVerificationResponse = AxiosResponse<MessageResponse>
259
+ export type VerifyEmailResponse = AxiosResponse<MessageResponse>
260
+ export type RefreshSessionResponse = AxiosResponse<AuthenticationResponse>
261
+ export type GetSessionsResponse = AxiosResponse<SessionListResponse>
262
+ export type DeleteSessionResponse = AxiosResponse<MessageResponse>
263
+ export type DeleteAllSessionsResponse = AxiosResponse<MessageResponse>
264
+ export type CleanupSessionsResponse = AxiosResponse<MessageResponse>
265
+ export type GetMethodsResponse = AxiosResponse<AvailableMethodsResponse>
266
+
267
+ // ============================================
268
+ // Helper Functions (exported for convenience)
269
+ // ============================================
270
+
271
+ /**
272
+ * Extract unified user from account info
273
+ */
274
+ export function accountToUser(account: AccountInfo | null): User | null {
275
+ if (account === null) return null
276
+
277
+ // Person account - most common case
278
+ if (account.person !== undefined) {
279
+ return {
280
+ id: account.person.id,
281
+ accountId: account.id,
282
+ name: account.person.name,
283
+ email: account.person.email,
284
+ type: account.account_type as AuthenticationAccountType,
285
+ roles: account.person.roles,
286
+ isActive: account.is_active,
287
+ isVerified: account.is_verified,
288
+ lastLogin: account.last_login,
289
+ }
290
+ }
291
+
292
+ // Entity account
293
+ if (account.entity !== undefined) {
294
+ return {
295
+ id: account.entity.id,
296
+ accountId: account.id,
297
+ name: account.entity.name,
298
+ type: account.account_type as AuthenticationAccountType,
299
+ isActive: account.is_active,
300
+ isVerified: account.is_verified,
301
+ lastLogin: account.last_login,
302
+ entityType: account.entity.type,
303
+ metadata: account.entity.metadata,
304
+ }
305
+ }
306
+
307
+ // Fallback - use account info directly
308
+ // Extract email from authentication methods
309
+ const emailMethod = account.authentication_methods?.find(
310
+ m => m.type === 'password' || m.type === 'email_token',
311
+ )
312
+
313
+ return {
314
+ id: account.id,
315
+ accountId: account.id,
316
+ name: account.display_name,
317
+ email: emailMethod?.identifier,
318
+ type: account.account_type as AuthenticationAccountType,
319
+ isActive: account.is_active,
320
+ isVerified: account.is_verified,
321
+ lastLogin: account.last_login,
322
+ }
323
+ }