@plyaz/auth 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/.github/pull_request_template.md +71 -0
- package/.github/workflows/deploy.yml +9 -0
- package/.github/workflows/publish.yml +14 -0
- package/.github/workflows/security.yml +20 -0
- package/README.md +89 -0
- package/commits.txt +5 -0
- package/dist/common/index.cjs +48 -0
- package/dist/common/index.cjs.map +1 -0
- package/dist/common/index.mjs +43 -0
- package/dist/common/index.mjs.map +1 -0
- package/dist/index.cjs +20411 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.mjs +5139 -0
- package/dist/index.mjs.map +1 -0
- package/eslint.config.mjs +13 -0
- package/index.html +13 -0
- package/package.json +141 -0
- package/src/adapters/auth-adapter-factory.ts +26 -0
- package/src/adapters/auth-adapter.mapper.ts +53 -0
- package/src/adapters/base-auth.adapter.ts +119 -0
- package/src/adapters/clerk/clerk.adapter.ts +204 -0
- package/src/adapters/custom/custom.adapter.ts +119 -0
- package/src/adapters/index.ts +4 -0
- package/src/adapters/next-auth/authOptions.ts +81 -0
- package/src/adapters/next-auth/next-auth.adapter.ts +211 -0
- package/src/api/client.ts +37 -0
- package/src/audit/audit.logger.ts +52 -0
- package/src/client/components/ProtectedRoute.tsx +37 -0
- package/src/client/hooks/useAuth.ts +128 -0
- package/src/client/hooks/useConnectedAccounts.ts +108 -0
- package/src/client/hooks/usePermissions.ts +36 -0
- package/src/client/hooks/useRBAC.ts +36 -0
- package/src/client/hooks/useSession.ts +18 -0
- package/src/client/providers/AuthProvider.tsx +104 -0
- package/src/client/store/auth.store.ts +306 -0
- package/src/client/utils/storage.ts +70 -0
- package/src/common/constants/oauth-providers.ts +49 -0
- package/src/common/errors/auth.errors.ts +64 -0
- package/src/common/errors/specific-auth-errors.ts +201 -0
- package/src/common/index.ts +19 -0
- package/src/common/regex/index.ts +27 -0
- package/src/common/types/auth.types.ts +641 -0
- package/src/common/types/index.ts +297 -0
- package/src/common/utils/index.ts +84 -0
- package/src/core/blacklist/token.blacklist.ts +60 -0
- package/src/core/index.ts +2 -0
- package/src/core/jwt/jwt.manager.ts +131 -0
- package/src/core/session/session.manager.ts +56 -0
- package/src/db/repositories/connected-account.repository.ts +415 -0
- package/src/db/repositories/role.repository.ts +519 -0
- package/src/db/repositories/session.repository.ts +308 -0
- package/src/db/repositories/user.repository.ts +320 -0
- package/src/flows/index.ts +2 -0
- package/src/flows/sign-in.flow.ts +106 -0
- package/src/flows/sign-up.flow.ts +121 -0
- package/src/index.ts +54 -0
- package/src/libs/clerk.helper.ts +36 -0
- package/src/libs/supabase.helper.ts +255 -0
- package/src/libs/supabaseClient.ts +6 -0
- package/src/providers/base/auth-provider.interface.ts +42 -0
- package/src/providers/base/index.ts +1 -0
- package/src/providers/index.ts +2 -0
- package/src/providers/oauth/facebook.provider.ts +97 -0
- package/src/providers/oauth/github.provider.ts +148 -0
- package/src/providers/oauth/google.provider.ts +126 -0
- package/src/providers/oauth/index.ts +3 -0
- package/src/rbac/dynamic-roles.ts +552 -0
- package/src/rbac/index.ts +4 -0
- package/src/rbac/permission-checker.ts +464 -0
- package/src/rbac/role-hierarchy.ts +545 -0
- package/src/rbac/role.manager.ts +75 -0
- package/src/security/csrf/csrf.protection.ts +37 -0
- package/src/security/index.ts +3 -0
- package/src/security/rate-limiting/auth/auth.controller.ts +12 -0
- package/src/security/rate-limiting/auth/rate-limiting.interface.ts +67 -0
- package/src/security/rate-limiting/auth.module.ts +32 -0
- package/src/server/auth.module.ts +158 -0
- package/src/server/decorators/auth.decorator.ts +43 -0
- package/src/server/decorators/auth.decorators.ts +31 -0
- package/src/server/decorators/current-user.decorator.ts +49 -0
- package/src/server/decorators/permission.decorator.ts +49 -0
- package/src/server/guards/auth.guard.ts +56 -0
- package/src/server/guards/custom-throttler.guard.ts +46 -0
- package/src/server/guards/permissions.guard.ts +115 -0
- package/src/server/guards/roles.guard.ts +31 -0
- package/src/server/middleware/auth.middleware.ts +46 -0
- package/src/server/middleware/index.ts +2 -0
- package/src/server/middleware/middleware.ts +11 -0
- package/src/server/middleware/session.middleware.ts +255 -0
- package/src/server/services/account.service.ts +269 -0
- package/src/server/services/auth.service.ts +79 -0
- package/src/server/services/brute-force.service.ts +98 -0
- package/src/server/services/index.ts +15 -0
- package/src/server/services/rate-limiter.service.ts +60 -0
- package/src/server/services/session.service.ts +287 -0
- package/src/server/services/token.service.ts +262 -0
- package/src/session/cookie-store.ts +255 -0
- package/src/session/enhanced-session-manager.ts +406 -0
- package/src/session/index.ts +14 -0
- package/src/session/memory-store.ts +320 -0
- package/src/session/redis-store.ts +443 -0
- package/src/strategies/oauth.strategy.ts +128 -0
- package/src/strategies/traditional-auth.strategy.ts +116 -0
- package/src/tokens/index.ts +4 -0
- package/src/tokens/refresh-token-manager.ts +448 -0
- package/src/tokens/token-validator.ts +311 -0
- package/tsconfig.build.json +28 -0
- package/tsconfig.json +38 -0
- package/tsup.config.mjs +28 -0
- package/vitest.config.mjs +16 -0
- package/vitest.setup.d.ts +2 -0
- package/vitest.setup.d.ts.map +1 -0
- package/vitest.setup.ts +1 -0
|
@@ -0,0 +1,641 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Core authentication types and interfaces for @plyaz/auth
|
|
3
|
+
* @module @plyaz/auth/types
|
|
4
|
+
*
|
|
5
|
+
* @description
|
|
6
|
+
* Defines all TypeScript interfaces, enums, and types for the authentication system.
|
|
7
|
+
* Includes B2C (public) and B2B (backoffice) user types, sessions, RBAC, and provider adapters.
|
|
8
|
+
* All types match database schema exactly (snake_case in DB, camelCase in TS).
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
// ============================================
|
|
12
|
+
// ENUMS
|
|
13
|
+
// ============================================
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* User role assignment status
|
|
17
|
+
* @enum {string}
|
|
18
|
+
*/
|
|
19
|
+
export enum USER_ROLE_STATUS {
|
|
20
|
+
/** Role is active and grants permissions */
|
|
21
|
+
ACTIVE = 'ACTIVE',
|
|
22
|
+
/** Role is inactive (temporarily disabled) */
|
|
23
|
+
INACTIVE = 'INACTIVE',
|
|
24
|
+
/** Role is suspended (user violation) */
|
|
25
|
+
SUSPENDED = 'SUSPENDED'
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Authentication provider types
|
|
30
|
+
* @enum {string}
|
|
31
|
+
*/
|
|
32
|
+
export enum AUTHPROVIDER {
|
|
33
|
+
/** Email/password authentication */
|
|
34
|
+
EMAIL = 'EMAIL',
|
|
35
|
+
/** Clerk authentication */
|
|
36
|
+
CLERK = 'CLERK',
|
|
37
|
+
/** Google OAuth */
|
|
38
|
+
GOOGLE = 'GOOGLE',
|
|
39
|
+
/** Facebook OAuth */
|
|
40
|
+
FACEBOOK = 'FACEBOOK',
|
|
41
|
+
/** Apple Sign In */
|
|
42
|
+
APPLE = 'APPLE',
|
|
43
|
+
/** Web3 wallet authentication */
|
|
44
|
+
WEB3 = 'WEB3'
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Token type for authentication
|
|
49
|
+
* @enum {string}
|
|
50
|
+
*/
|
|
51
|
+
export enum TOKENTYPE {
|
|
52
|
+
/** Bearer token */
|
|
53
|
+
BEARER = 'Bearer',
|
|
54
|
+
/** JSON Web Token */
|
|
55
|
+
JWT = 'JWT'
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// ============================================
|
|
59
|
+
// CORE USER TYPES
|
|
60
|
+
// ============================================
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* B2C User (public schema)
|
|
64
|
+
* Represents platform users: fans, athletes, clubs, scouts, agents
|
|
65
|
+
*
|
|
66
|
+
* @interface User
|
|
67
|
+
* @property {string} id - Unique user identifier (UUID)
|
|
68
|
+
* @property {string} email - User email address (unique)
|
|
69
|
+
* @property {string} [clerkUserId] - Clerk provider user ID
|
|
70
|
+
* @property {string} authProvider - Authentication provider used
|
|
71
|
+
* @property {string} [firstName] - User first name
|
|
72
|
+
* @property {string} [lastName] - User last name
|
|
73
|
+
* @property {string} displayName - Display name (required)
|
|
74
|
+
* @property {string} [avatarUrl] - Avatar image URL
|
|
75
|
+
* @property {string} [phoneNumber] - Phone number
|
|
76
|
+
* @property {boolean} isActive - Account active status
|
|
77
|
+
* @property {boolean} isVerified - Email verification status
|
|
78
|
+
* @property {Date} createdAt - Account creation timestamp
|
|
79
|
+
* @property {Date} updatedAt - Last update timestamp
|
|
80
|
+
* @property {Date} [lastLoginAt] - Last login timestamp
|
|
81
|
+
*/
|
|
82
|
+
export interface User {
|
|
83
|
+
id: string;
|
|
84
|
+
email: string;
|
|
85
|
+
clerkUserId?: string;
|
|
86
|
+
authProvider: string;
|
|
87
|
+
firstName?: string;
|
|
88
|
+
lastName?: string;
|
|
89
|
+
displayName: string;
|
|
90
|
+
avatarUrl?: string;
|
|
91
|
+
phoneNumber?: string;
|
|
92
|
+
isActive: boolean;
|
|
93
|
+
isVerified: boolean;
|
|
94
|
+
createdAt: Date;
|
|
95
|
+
updatedAt: Date;
|
|
96
|
+
lastLoginAt?: Date;
|
|
97
|
+
roles?: string[];
|
|
98
|
+
passwordHash?: string;
|
|
99
|
+
isSuspended?: boolean;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* B2B User (backoffice schema)
|
|
104
|
+
* Represents internal staff: admins, moderators, support, finance, compliance
|
|
105
|
+
*
|
|
106
|
+
* @interface BackofficeUser
|
|
107
|
+
* @property {string} id - Unique user identifier (UUID)
|
|
108
|
+
* @property {string} email - User email address (unique)
|
|
109
|
+
* @property {string} passwordHash - Hashed password
|
|
110
|
+
* @property {string} [clerkUserId] - Clerk provider user ID
|
|
111
|
+
* @property {string} authProvider - Authentication provider used
|
|
112
|
+
* @property {string} [firstName] - User first name
|
|
113
|
+
* @property {string} [lastName] - User last name
|
|
114
|
+
* @property {string} displayName - Display name (required)
|
|
115
|
+
* @property {string} [avatarMediaId] - Avatar media UUID reference
|
|
116
|
+
* @property {string} [phoneNumber] - Phone number
|
|
117
|
+
* @property {boolean} isActive - Account active status
|
|
118
|
+
* @property {boolean} isVerified - Email verification status
|
|
119
|
+
* @property {boolean} isSuspended - Account suspension status
|
|
120
|
+
* @property {string} [suspensionReason] - Reason for suspension
|
|
121
|
+
* @property {Date} [suspendedAt] - Suspension timestamp
|
|
122
|
+
* @property {Date} createdAt - Account creation timestamp
|
|
123
|
+
* @property {Date} updatedAt - Last update timestamp
|
|
124
|
+
* @property {Date} [lastLoginAt] - Last login timestamp
|
|
125
|
+
*/
|
|
126
|
+
export interface BackofficeUser {
|
|
127
|
+
id: string;
|
|
128
|
+
email: string;
|
|
129
|
+
passwordHash: string;
|
|
130
|
+
clerkUserId?: string;
|
|
131
|
+
authProvider: string;
|
|
132
|
+
firstName?: string;
|
|
133
|
+
lastName?: string;
|
|
134
|
+
displayName: string;
|
|
135
|
+
avatarMediaId?: string;
|
|
136
|
+
phoneNumber?: string;
|
|
137
|
+
isActive: boolean;
|
|
138
|
+
isVerified: boolean;
|
|
139
|
+
isSuspended: boolean;
|
|
140
|
+
suspensionReason?: string;
|
|
141
|
+
suspendedAt?: Date;
|
|
142
|
+
createdAt: Date;
|
|
143
|
+
updatedAt: Date;
|
|
144
|
+
lastLoginAt?: Date;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// ============================================
|
|
148
|
+
// SESSION TYPES
|
|
149
|
+
// ============================================
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* B2C Session (public schema)
|
|
153
|
+
* Tracks authenticated user sessions with device and activity info
|
|
154
|
+
*
|
|
155
|
+
* @interface Session
|
|
156
|
+
*/
|
|
157
|
+
export interface Session {
|
|
158
|
+
id: string;
|
|
159
|
+
userId: string;
|
|
160
|
+
provider: string;
|
|
161
|
+
providerSessionId?: string;
|
|
162
|
+
expiresAt: Date;
|
|
163
|
+
createdAt: Date;
|
|
164
|
+
lastActivityAt: Date;
|
|
165
|
+
ipAddress?: string;
|
|
166
|
+
userAgent?: string;
|
|
167
|
+
metadata?: Record<string, string>;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* B2B Session (backoffice schema)
|
|
172
|
+
* Tracks authenticated backoffice user sessions
|
|
173
|
+
*
|
|
174
|
+
* @interface BackofficeSession
|
|
175
|
+
*/
|
|
176
|
+
export interface BackofficeSession {
|
|
177
|
+
id: string;
|
|
178
|
+
backofficeUserId: string;
|
|
179
|
+
provider: string;
|
|
180
|
+
providerSessionId?: string;
|
|
181
|
+
expiresAt: Date;
|
|
182
|
+
createdAt: Date;
|
|
183
|
+
lastActivityAt: Date;
|
|
184
|
+
ipAddress?: string;
|
|
185
|
+
userAgent?: string;
|
|
186
|
+
metadata?: Record<string, string>;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// ============================================
|
|
190
|
+
// CONNECTED ACCOUNT TYPES
|
|
191
|
+
// ============================================
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Connected Account (provider linking)
|
|
195
|
+
* Links external OAuth/Web3 provider accounts to users
|
|
196
|
+
* Supports OAuth providers (Clerk, Google, etc.) and Web3 wallets
|
|
197
|
+
*
|
|
198
|
+
* @interface ConnectedAccount
|
|
199
|
+
*/
|
|
200
|
+
export interface ConnectedAccount {
|
|
201
|
+
id: string;
|
|
202
|
+
userId: string;
|
|
203
|
+
providerType: string;
|
|
204
|
+
provider: string;
|
|
205
|
+
providerAccountId: string;
|
|
206
|
+
providerEmail?: string;
|
|
207
|
+
providerUsername?: string;
|
|
208
|
+
providerDisplayName?: string;
|
|
209
|
+
providerAvatarUrl?: string;
|
|
210
|
+
providerProfileUrl?: string;
|
|
211
|
+
providerMetadata?: Record<string, unknown>;
|
|
212
|
+
walletAddress?: string;
|
|
213
|
+
chainId?: string;
|
|
214
|
+
accessTokenEncrypted?: string;
|
|
215
|
+
refreshTokenEncrypted?: string;
|
|
216
|
+
tokenExpiresAt?: Date;
|
|
217
|
+
tokenScope?: string;
|
|
218
|
+
isPrimary: boolean;
|
|
219
|
+
isVerified: boolean;
|
|
220
|
+
isActive: boolean;
|
|
221
|
+
linkedAt: Date;
|
|
222
|
+
linkedIpAddress?: string;
|
|
223
|
+
linkedUserAgent?: string;
|
|
224
|
+
lastUsedAt?: Date;
|
|
225
|
+
lastUsedIpAddress?: string;
|
|
226
|
+
createdAt: Date;
|
|
227
|
+
updatedAt: Date;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
// ============================================
|
|
231
|
+
// AUTH TOKENS
|
|
232
|
+
// ============================================
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* Authentication tokens returned after successful login
|
|
236
|
+
*
|
|
237
|
+
* @interface AuthTokens
|
|
238
|
+
*/
|
|
239
|
+
export interface AuthTokens {
|
|
240
|
+
accessToken: string;
|
|
241
|
+
refreshToken: string;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
// ============================================
|
|
245
|
+
// RBAC TYPES
|
|
246
|
+
// ============================================
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* B2C Role (public schema)
|
|
250
|
+
* Defines user roles: FAN, ATHLETE, SCOUT, AGENT, CLUB, DEVELOPER, ADMIN
|
|
251
|
+
*
|
|
252
|
+
* @interface Role
|
|
253
|
+
*/
|
|
254
|
+
export interface Role {
|
|
255
|
+
id: string;
|
|
256
|
+
code: string;
|
|
257
|
+
name: string;
|
|
258
|
+
description?: string;
|
|
259
|
+
hierarchy: number;
|
|
260
|
+
canCreateCampaigns?: boolean;
|
|
261
|
+
canContribute?: boolean;
|
|
262
|
+
requiresKyc?: boolean;
|
|
263
|
+
isActive: boolean;
|
|
264
|
+
isSystem: boolean;
|
|
265
|
+
metadata?: Record<string, string>;
|
|
266
|
+
createdAt: Date;
|
|
267
|
+
updatedAt: Date;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* B2B Role (backoffice schema)
|
|
272
|
+
* Defines staff roles: SUPER_ADMIN, ADMIN, MODERATOR, FINANCE, COMPLIANCE, SUPPORT
|
|
273
|
+
*
|
|
274
|
+
* @interface BackofficeRole
|
|
275
|
+
*/
|
|
276
|
+
export interface BackofficeRole {
|
|
277
|
+
id: string;
|
|
278
|
+
code: string;
|
|
279
|
+
name: string;
|
|
280
|
+
description?: string;
|
|
281
|
+
hierarchy: number;
|
|
282
|
+
canApproveCampaigns: boolean;
|
|
283
|
+
canApproveKyc: boolean;
|
|
284
|
+
canApprovePayouts: boolean;
|
|
285
|
+
canManageUsers: boolean;
|
|
286
|
+
canManageRoles: boolean;
|
|
287
|
+
canViewAllData: boolean;
|
|
288
|
+
isActive: boolean;
|
|
289
|
+
isSystem: boolean;
|
|
290
|
+
metadata?: Record<string, string>;
|
|
291
|
+
createdAt: Date;
|
|
292
|
+
updatedAt: Date;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* Permission (backoffice only)
|
|
297
|
+
* Fine-grained permissions for backoffice users
|
|
298
|
+
*
|
|
299
|
+
* @interface Permission
|
|
300
|
+
*/
|
|
301
|
+
export interface Permission {
|
|
302
|
+
id: string;
|
|
303
|
+
code: string;
|
|
304
|
+
name: string;
|
|
305
|
+
description?: string;
|
|
306
|
+
resource: string;
|
|
307
|
+
action: string;
|
|
308
|
+
isActive: boolean;
|
|
309
|
+
isSystem: boolean;
|
|
310
|
+
metadata?: Record<string, string>;
|
|
311
|
+
createdAt: Date;
|
|
312
|
+
updatedAt: Date;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* Role-Permission mapping (backoffice only)
|
|
317
|
+
* Links permissions to roles
|
|
318
|
+
*
|
|
319
|
+
* @interface RolePermission
|
|
320
|
+
*/
|
|
321
|
+
export interface RolePermission {
|
|
322
|
+
id: string;
|
|
323
|
+
roleId: string;
|
|
324
|
+
role: string;
|
|
325
|
+
permissionId: string;
|
|
326
|
+
grantedAt: Date;
|
|
327
|
+
grantedBy?: string;
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/**
|
|
331
|
+
* User-Permission mapping (backoffice only)
|
|
332
|
+
* Grants/revokes specific permissions to users
|
|
333
|
+
*
|
|
334
|
+
* @interface UserPermission
|
|
335
|
+
*/
|
|
336
|
+
export interface UserPermission {
|
|
337
|
+
id: string;
|
|
338
|
+
backofficeUserId: string;
|
|
339
|
+
permissionId: string;
|
|
340
|
+
isGranted: boolean;
|
|
341
|
+
expiresAt?: Date;
|
|
342
|
+
grantedAt: Date;
|
|
343
|
+
grantedBy?: string;
|
|
344
|
+
reason?: string;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
/**
|
|
348
|
+
* B2C User-Role assignment
|
|
349
|
+
* Links users to roles with status tracking
|
|
350
|
+
*
|
|
351
|
+
* @interface UserRole
|
|
352
|
+
*/
|
|
353
|
+
export interface UserRole {
|
|
354
|
+
id: string;
|
|
355
|
+
userId: string;
|
|
356
|
+
roleId: string;
|
|
357
|
+
role: string;
|
|
358
|
+
isPrimary: boolean;
|
|
359
|
+
status: USER_ROLE_STATUS;
|
|
360
|
+
assignedBy?: string;
|
|
361
|
+
assignedReason?: string;
|
|
362
|
+
expiresAt?: Date;
|
|
363
|
+
createdAt: Date;
|
|
364
|
+
updatedAt: Date;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
/**
|
|
368
|
+
* B2B User-Role assignment
|
|
369
|
+
* Links backoffice users to roles
|
|
370
|
+
*
|
|
371
|
+
* @interface BackofficeUserRole
|
|
372
|
+
*/
|
|
373
|
+
export interface BackofficeUserRole {
|
|
374
|
+
id: string;
|
|
375
|
+
backofficeUserId: string;
|
|
376
|
+
roleId: string;
|
|
377
|
+
role: string;
|
|
378
|
+
isPrimary: boolean;
|
|
379
|
+
status: USER_ROLE_STATUS;
|
|
380
|
+
assignedBy?: string;
|
|
381
|
+
assignedReason?: string;
|
|
382
|
+
expiresAt?: Date;
|
|
383
|
+
createdAt: Date;
|
|
384
|
+
updatedAt: Date;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
// ============================================
|
|
388
|
+
// AUTH PROVIDER ADAPTER INTERFACE
|
|
389
|
+
// ============================================
|
|
390
|
+
|
|
391
|
+
/**
|
|
392
|
+
* Authentication provider adapter interface
|
|
393
|
+
* Defines contract for provider-agnostic authentication
|
|
394
|
+
*
|
|
395
|
+
* @interface AuthProviderAdapter
|
|
396
|
+
* @example
|
|
397
|
+
* ```typescript
|
|
398
|
+
* class ClerkAdapter implements AuthProviderAdapter {
|
|
399
|
+
* name = 'clerk';
|
|
400
|
+
* async verifyToken(token: string) { ... }
|
|
401
|
+
* async getUserInfo(token: string) { ... }
|
|
402
|
+
* }
|
|
403
|
+
* ```
|
|
404
|
+
*/
|
|
405
|
+
export interface AuthProviderAdapter {
|
|
406
|
+
name: string;
|
|
407
|
+
|
|
408
|
+
verifyToken(token: string): Promise<VerifiedToken>;
|
|
409
|
+
|
|
410
|
+
getUserInfo(token: string): Promise<ProviderUserInfo>;
|
|
411
|
+
|
|
412
|
+
refreshToken?(refreshToken: string): Promise<AuthTokens>;
|
|
413
|
+
|
|
414
|
+
revokeToken?(token: string): Promise<void>;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
/**
|
|
418
|
+
* Verified token result
|
|
419
|
+
* Returned after successful token verification
|
|
420
|
+
*
|
|
421
|
+
* @interface VerifiedToken
|
|
422
|
+
*/
|
|
423
|
+
export interface VerifiedToken {
|
|
424
|
+
userId: string;
|
|
425
|
+
provider: string;
|
|
426
|
+
providerAccountId: string;
|
|
427
|
+
email?: string;
|
|
428
|
+
expiresAt?: Date;
|
|
429
|
+
metadata?: Record<string, string>;
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
/**
|
|
433
|
+
* Provider user information
|
|
434
|
+
* User profile data from external provider
|
|
435
|
+
*
|
|
436
|
+
* @interface ProviderUserInfo
|
|
437
|
+
*/
|
|
438
|
+
export interface ProviderUserInfo {
|
|
439
|
+
providerAccountId: string;
|
|
440
|
+
email?: string;
|
|
441
|
+
displayName?: string;
|
|
442
|
+
firstName?: string;
|
|
443
|
+
lastName?: string;
|
|
444
|
+
avatarUrl?: string;
|
|
445
|
+
phoneNumber?: string;
|
|
446
|
+
metadata?: Record<string, string>;
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
// ============================================
|
|
450
|
+
// REPOSITORY INTERFACES
|
|
451
|
+
// ============================================
|
|
452
|
+
|
|
453
|
+
/**
|
|
454
|
+
* User repository interface
|
|
455
|
+
* Defines data access methods for user management
|
|
456
|
+
*
|
|
457
|
+
* @interface UserRepository
|
|
458
|
+
*/
|
|
459
|
+
export interface UserRepository {
|
|
460
|
+
findById(id: string): Promise<User | null>;
|
|
461
|
+
findByEmail(email: string): Promise<User | null>;
|
|
462
|
+
findByProviderAccount(provider: string, providerAccountId: string): Promise<User | null>;
|
|
463
|
+
findByCredentials(email: string, passwordHash: string): Promise<User | null>;
|
|
464
|
+
create(data: CreateUserData): Promise<User>;
|
|
465
|
+
update(id: string, data: UpdateUserData): Promise<User>;
|
|
466
|
+
delete(id: string): Promise<void>;
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
/**
|
|
470
|
+
* Session repository interface
|
|
471
|
+
* Defines data access methods for session management
|
|
472
|
+
*
|
|
473
|
+
* @interface SessionRepository
|
|
474
|
+
*/
|
|
475
|
+
export interface SessionRepository {
|
|
476
|
+
create(data: CreateSessionData): Promise<Session>;
|
|
477
|
+
findById(id: string): Promise<Session | null>;
|
|
478
|
+
findByUserId(userId: string): Promise<Session[]>;
|
|
479
|
+
validate(sessionId: string): Promise<Session | null>;
|
|
480
|
+
invalidate(sessionId: string): Promise<void>;
|
|
481
|
+
invalidateAllForUser(userId: string): Promise<void>;
|
|
482
|
+
updateActivity(sessionId: string): Promise<void>;
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
/**
|
|
486
|
+
* Connected account repository interface
|
|
487
|
+
* Defines data access methods for provider account linking
|
|
488
|
+
*
|
|
489
|
+
* @interface ConnectedAccountRepository
|
|
490
|
+
*/
|
|
491
|
+
export interface ConnectedAccountRepository {
|
|
492
|
+
create(data: CreateConnectedAccountData): Promise<ConnectedAccount>;
|
|
493
|
+
findById(id: string): Promise<ConnectedAccount | null>;
|
|
494
|
+
findByUserId(userId: string): Promise<ConnectedAccount[]>;
|
|
495
|
+
findByProvider(provider: string, providerAccountId: string): Promise<ConnectedAccount | null>;
|
|
496
|
+
update(id: string, data: UpdateConnectedAccountData): Promise<ConnectedAccount>;
|
|
497
|
+
delete(id: string): Promise<void>;
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
// ============================================
|
|
501
|
+
// DATA TRANSFER OBJECTS
|
|
502
|
+
// ============================================
|
|
503
|
+
|
|
504
|
+
/**
|
|
505
|
+
* DTO for creating B2C users
|
|
506
|
+
* @interface CreateUserData
|
|
507
|
+
*/
|
|
508
|
+
export interface CreateUserData {
|
|
509
|
+
email: string;
|
|
510
|
+
clerkUserId?: string;
|
|
511
|
+
authProvider?: string;
|
|
512
|
+
firstName?: string;
|
|
513
|
+
lastName?: string;
|
|
514
|
+
displayName: string;
|
|
515
|
+
avatarUrl?: string;
|
|
516
|
+
phoneNumber?: string;
|
|
517
|
+
isVerified?: boolean;
|
|
518
|
+
passwordHash?: string;
|
|
519
|
+
isActive?: boolean;
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
/**
|
|
523
|
+
* DTO for creating B2B users
|
|
524
|
+
* @interface CreateBackofficeUserData
|
|
525
|
+
*/
|
|
526
|
+
export interface CreateBackofficeUserData {
|
|
527
|
+
email: string;
|
|
528
|
+
passwordHash: string;
|
|
529
|
+
clerkUserId?: string;
|
|
530
|
+
authProvider?: string;
|
|
531
|
+
firstName?: string;
|
|
532
|
+
lastName?: string;
|
|
533
|
+
displayName: string;
|
|
534
|
+
avatarMediaId?: string;
|
|
535
|
+
phoneNumber?: string;
|
|
536
|
+
isVerified?: boolean;
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
/**
|
|
540
|
+
* DTO for updating B2C users
|
|
541
|
+
* @interface UpdateUserData
|
|
542
|
+
*/
|
|
543
|
+
export interface UpdateUserData {
|
|
544
|
+
email?: string;
|
|
545
|
+
clerkUserId?: string;
|
|
546
|
+
authProvider?: string;
|
|
547
|
+
firstName?: string;
|
|
548
|
+
lastName?: string;
|
|
549
|
+
displayName?: string;
|
|
550
|
+
avatarUrl?: string;
|
|
551
|
+
phoneNumber?: string;
|
|
552
|
+
isActive?: boolean;
|
|
553
|
+
isVerified?: boolean;
|
|
554
|
+
lastLoginAt?: Date;
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
/**
|
|
558
|
+
* DTO for updating B2B users
|
|
559
|
+
* @interface UpdateBackofficeUserData
|
|
560
|
+
*/
|
|
561
|
+
export interface UpdateBackofficeUserData {
|
|
562
|
+
email?: string;
|
|
563
|
+
passwordHash?: string;
|
|
564
|
+
clerkUserId?: string;
|
|
565
|
+
authProvider?: string;
|
|
566
|
+
firstName?: string;
|
|
567
|
+
lastName?: string;
|
|
568
|
+
displayName?: string;
|
|
569
|
+
avatarMediaId?: string;
|
|
570
|
+
phoneNumber?: string;
|
|
571
|
+
isActive?: boolean;
|
|
572
|
+
isVerified?: boolean;
|
|
573
|
+
isSuspended?: boolean;
|
|
574
|
+
suspensionReason?: string;
|
|
575
|
+
suspendedAt?: Date;
|
|
576
|
+
lastLoginAt?: Date;
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
/**
|
|
580
|
+
* DTO for creating sessions
|
|
581
|
+
* @interface CreateSessionData
|
|
582
|
+
*/
|
|
583
|
+
export interface CreateSessionData {
|
|
584
|
+
userId: string;
|
|
585
|
+
provider: string;
|
|
586
|
+
providerSessionId?: string;
|
|
587
|
+
expiresAt: Date;
|
|
588
|
+
ipAddress?: string;
|
|
589
|
+
userAgent?: string;
|
|
590
|
+
metadata?: Record<string, string>;
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
/**
|
|
594
|
+
* DTO for creating connected accounts
|
|
595
|
+
* @interface CreateConnectedAccountData
|
|
596
|
+
*/
|
|
597
|
+
export interface CreateConnectedAccountData {
|
|
598
|
+
userId: string;
|
|
599
|
+
providerType: string;
|
|
600
|
+
provider: string;
|
|
601
|
+
providerAccountId: string;
|
|
602
|
+
providerEmail?: string;
|
|
603
|
+
providerUsername?: string;
|
|
604
|
+
providerDisplayName?: string;
|
|
605
|
+
providerAvatarUrl?: string;
|
|
606
|
+
providerProfileUrl?: string;
|
|
607
|
+
providerMetadata?: Record<string, unknown>;
|
|
608
|
+
walletAddress?: string;
|
|
609
|
+
chainId?: string;
|
|
610
|
+
accessTokenEncrypted?: string;
|
|
611
|
+
refreshTokenEncrypted?: string;
|
|
612
|
+
tokenExpiresAt?: Date;
|
|
613
|
+
tokenScope?: string;
|
|
614
|
+
isPrimary?: boolean;
|
|
615
|
+
isVerified?: boolean;
|
|
616
|
+
isActive?: boolean;
|
|
617
|
+
linkedIpAddress?: string;
|
|
618
|
+
linkedUserAgent?: string;
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
/**
|
|
622
|
+
* DTO for updating connected accounts
|
|
623
|
+
* @interface UpdateConnectedAccountData
|
|
624
|
+
*/
|
|
625
|
+
export interface UpdateConnectedAccountData {
|
|
626
|
+
providerEmail?: string;
|
|
627
|
+
providerUsername?: string;
|
|
628
|
+
providerDisplayName?: string;
|
|
629
|
+
providerAvatarUrl?: string;
|
|
630
|
+
providerProfileUrl?: string;
|
|
631
|
+
providerMetadata?: Record<string, string>;
|
|
632
|
+
accessTokenEncrypted?: string;
|
|
633
|
+
refreshTokenEncrypted?: string;
|
|
634
|
+
tokenExpiresAt?: Date;
|
|
635
|
+
tokenScope?: string;
|
|
636
|
+
isPrimary?: boolean;
|
|
637
|
+
isVerified?: boolean;
|
|
638
|
+
isActive?: boolean;
|
|
639
|
+
lastUsedAt?: Date;
|
|
640
|
+
lastUsedIpAddress?: string;
|
|
641
|
+
}
|