@cauth/express 0.0.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/README.md +140 -0
- package/dist/index.cjs +28 -0
- package/dist/index.d.cts +341 -0
- package/package.json +43 -0
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,341 @@
|
|
|
1
|
+
import * as express0 from "express";
|
|
2
|
+
import z$1, { z } from "zod";
|
|
3
|
+
import ms from "ms";
|
|
4
|
+
|
|
5
|
+
//#region ../core/src/types/auth.t.d.ts
|
|
6
|
+
type Account = {
|
|
7
|
+
id: string;
|
|
8
|
+
phoneNumber: string;
|
|
9
|
+
email: string;
|
|
10
|
+
role: string;
|
|
11
|
+
lastLogin: Date;
|
|
12
|
+
createdAt: Date;
|
|
13
|
+
updatedAt: Date;
|
|
14
|
+
};
|
|
15
|
+
type Tokens = {
|
|
16
|
+
accessToken: string;
|
|
17
|
+
refreshToken: string;
|
|
18
|
+
};
|
|
19
|
+
declare const AuthModelSchema: z$1.ZodObject<{
|
|
20
|
+
id: z$1.ZodString;
|
|
21
|
+
phoneNumber: z$1.ZodString;
|
|
22
|
+
email: z$1.ZodString;
|
|
23
|
+
passwordHash: z$1.ZodOptional<z$1.ZodString>;
|
|
24
|
+
role: z$1.ZodString;
|
|
25
|
+
lastLogin: z$1.ZodDate;
|
|
26
|
+
refreshTokens: z$1.ZodOptional<z$1.ZodArray<z$1.ZodString>>;
|
|
27
|
+
createdAt: z$1.ZodDate;
|
|
28
|
+
updatedAt: z$1.ZodDate;
|
|
29
|
+
}, z$1.z.core.$strip>;
|
|
30
|
+
type AuthModel = z$1.infer<typeof AuthModelSchema>;
|
|
31
|
+
//#endregion
|
|
32
|
+
//#region ../core/src/types/result.t.d.ts
|
|
33
|
+
type FNError = {
|
|
34
|
+
type: string;
|
|
35
|
+
error: Error;
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* @description Core Result type.
|
|
39
|
+
* @template T - The type of the value.
|
|
40
|
+
* @template E - The type of the errors, which must extend { type: string; error: Error }.
|
|
41
|
+
*/
|
|
42
|
+
type Result$1<T, E extends FNError = FNError> = {
|
|
43
|
+
success: true;
|
|
44
|
+
value: T;
|
|
45
|
+
} | {
|
|
46
|
+
success: false;
|
|
47
|
+
errors: E[];
|
|
48
|
+
};
|
|
49
|
+
//#endregion
|
|
50
|
+
//#region ../core/src/types/otp-purpose.t.d.ts
|
|
51
|
+
type OtpPurpose = 'LOGIN' | 'RESET_PASSWORD' | 'ACTION';
|
|
52
|
+
//#endregion
|
|
53
|
+
//#region ../core/src/types/database.contract.d.ts
|
|
54
|
+
interface DatabaseContract {
|
|
55
|
+
findAccountById<T = AuthModel>({
|
|
56
|
+
...args
|
|
57
|
+
}: {
|
|
58
|
+
id: string;
|
|
59
|
+
select?: any;
|
|
60
|
+
}): Promise<T | undefined>;
|
|
61
|
+
findAccountWithCredential<T = AuthModel>({
|
|
62
|
+
...args
|
|
63
|
+
}: {
|
|
64
|
+
email?: string | undefined;
|
|
65
|
+
phoneNumber?: string | undefined;
|
|
66
|
+
select?: any;
|
|
67
|
+
}): Promise<T | undefined>;
|
|
68
|
+
createAccount<T = AuthModel>({
|
|
69
|
+
...args
|
|
70
|
+
}: {
|
|
71
|
+
data: any;
|
|
72
|
+
select?: any;
|
|
73
|
+
}): Promise<T>;
|
|
74
|
+
updateAccount<T = AuthModel>({
|
|
75
|
+
...args
|
|
76
|
+
}: {
|
|
77
|
+
id: string;
|
|
78
|
+
data: any;
|
|
79
|
+
select?: any;
|
|
80
|
+
}): Promise<T>;
|
|
81
|
+
updateAccountLogin<T = AuthModel>({
|
|
82
|
+
...args
|
|
83
|
+
}: {
|
|
84
|
+
id: string;
|
|
85
|
+
refreshToken: string;
|
|
86
|
+
select?: any;
|
|
87
|
+
}): Promise<T>;
|
|
88
|
+
removeAndAddRefreshToken({
|
|
89
|
+
...args
|
|
90
|
+
}: {
|
|
91
|
+
id: string;
|
|
92
|
+
refreshToken: string;
|
|
93
|
+
newRefreshToken?: string;
|
|
94
|
+
select?: any;
|
|
95
|
+
}): Promise<any>;
|
|
96
|
+
deleteAccount({
|
|
97
|
+
...args
|
|
98
|
+
}: {
|
|
99
|
+
id: string;
|
|
100
|
+
}): Promise<void>;
|
|
101
|
+
createOTP<T = {
|
|
102
|
+
code: string;
|
|
103
|
+
purpose: string;
|
|
104
|
+
expiresAt: Date;
|
|
105
|
+
}>({
|
|
106
|
+
config
|
|
107
|
+
}: {
|
|
108
|
+
config: CAuthOptions;
|
|
109
|
+
}, {
|
|
110
|
+
...args
|
|
111
|
+
}: {
|
|
112
|
+
id: string;
|
|
113
|
+
purpose: OtpPurpose;
|
|
114
|
+
}): Promise<T>;
|
|
115
|
+
verifyOTP<T = {
|
|
116
|
+
isValid: boolean;
|
|
117
|
+
}>({
|
|
118
|
+
...args
|
|
119
|
+
}: {
|
|
120
|
+
id: string;
|
|
121
|
+
code: string;
|
|
122
|
+
purpose: OtpPurpose;
|
|
123
|
+
}): Promise<T>;
|
|
124
|
+
}
|
|
125
|
+
//#endregion
|
|
126
|
+
//#region ../core/src/types/config.t.d.ts
|
|
127
|
+
declare const CAuthOptionsSchema: z$1.ZodObject<{
|
|
128
|
+
dbContractor: z$1.ZodCustom<DatabaseContract, DatabaseContract>;
|
|
129
|
+
routeContractor: z$1.ZodCustom<RoutesContract, RoutesContract>;
|
|
130
|
+
roles: z$1.ZodArray<z$1.ZodString>;
|
|
131
|
+
jwtConfig: z$1.ZodObject<{
|
|
132
|
+
refreshTokenSecret: z$1.ZodString;
|
|
133
|
+
accessTokenSecret: z$1.ZodString;
|
|
134
|
+
accessTokenLifeSpan: z$1.ZodOptional<z$1.ZodCustom<ms.StringValue, ms.StringValue>>;
|
|
135
|
+
refreshTokenLifeSpan: z$1.ZodOptional<z$1.ZodCustom<ms.StringValue, ms.StringValue>>;
|
|
136
|
+
}, z$1.z.core.$strip>;
|
|
137
|
+
otpConfig: z$1.ZodObject<{
|
|
138
|
+
expiresIn: z$1.ZodOptional<z$1.ZodNumber>;
|
|
139
|
+
length: z$1.ZodOptional<z$1.ZodNumber>;
|
|
140
|
+
}, z$1.z.core.$strip>;
|
|
141
|
+
}, z$1.z.core.$strip>;
|
|
142
|
+
type CAuthOptions = z$1.infer<typeof CAuthOptionsSchema>;
|
|
143
|
+
//#endregion
|
|
144
|
+
//#region ../core/src/types/dto-schemas.t.d.ts
|
|
145
|
+
declare const LoginSchema: z.ZodUnion<readonly [z.ZodObject<{
|
|
146
|
+
email: z.ZodEmail;
|
|
147
|
+
phoneNumber: z.ZodOptional<z.ZodNever>;
|
|
148
|
+
password: z.ZodString;
|
|
149
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
150
|
+
phoneNumber: z.ZodPipe<z.ZodString, z.ZodTransform<string, string>>;
|
|
151
|
+
email: z.ZodOptional<z.ZodNever>;
|
|
152
|
+
password: z.ZodString;
|
|
153
|
+
}, z.core.$strip>]>;
|
|
154
|
+
type LoginSchemaType = z.infer<typeof LoginSchema>;
|
|
155
|
+
declare const RegisterSchema: z.ZodObject<{
|
|
156
|
+
phoneNumber: z.ZodOptional<z.ZodPipe<z.ZodString, z.ZodTransform<string, string>>>;
|
|
157
|
+
email: z.ZodOptional<z.ZodEmail>;
|
|
158
|
+
role: z.ZodString;
|
|
159
|
+
password: z.ZodString;
|
|
160
|
+
}, z.core.$strip>;
|
|
161
|
+
type RegisterSchemaType = z.infer<typeof RegisterSchema>;
|
|
162
|
+
declare const RefreshTokenSchema: z.ZodObject<{
|
|
163
|
+
refreshToken: z.ZodString;
|
|
164
|
+
}, z.core.$strip>;
|
|
165
|
+
type RefreshTokenSchemaType = z.infer<typeof RefreshTokenSchema>;
|
|
166
|
+
declare const LogoutSchema: z.ZodObject<{
|
|
167
|
+
refreshToken: z.ZodString;
|
|
168
|
+
}, z.core.$strip>;
|
|
169
|
+
type LogoutSchemaType = z.infer<typeof LogoutSchema>;
|
|
170
|
+
declare const ChangePasswordSchema: z.ZodObject<{
|
|
171
|
+
accountId: z.ZodString;
|
|
172
|
+
oldPassword: z.ZodString;
|
|
173
|
+
newPassword: z.ZodString;
|
|
174
|
+
}, z.core.$strip>;
|
|
175
|
+
type ChangePasswordSchemaType = z.infer<typeof ChangePasswordSchema>;
|
|
176
|
+
//#endregion
|
|
177
|
+
//#region ../core/src/cauth.d.ts
|
|
178
|
+
declare class _CAuth<T extends string[]> {
|
|
179
|
+
#private;
|
|
180
|
+
constructor(config: Omit<CAuthOptions, 'roles'> & {
|
|
181
|
+
roles: T;
|
|
182
|
+
});
|
|
183
|
+
get RoleType(): T[number];
|
|
184
|
+
/**
|
|
185
|
+
* @description Authentication Guard Middleware. Include 'roles' for a custom auth guard.
|
|
186
|
+
*
|
|
187
|
+
* If 'roles' is empty it allows all authenticated users, without respecting specific role
|
|
188
|
+
*
|
|
189
|
+
* @default undefined
|
|
190
|
+
*/
|
|
191
|
+
Guard: (roles?: Array<T[number]>) => any;
|
|
192
|
+
Routes: {
|
|
193
|
+
Register: () => any;
|
|
194
|
+
Login: () => any;
|
|
195
|
+
Logout: () => any;
|
|
196
|
+
Refresh: () => any;
|
|
197
|
+
ChangePassword: (userId: string) => any;
|
|
198
|
+
};
|
|
199
|
+
FN: {
|
|
200
|
+
Login: ({
|
|
201
|
+
...args
|
|
202
|
+
}: LoginSchemaType) => Promise<Result$1<{
|
|
203
|
+
account: Account;
|
|
204
|
+
tokens: Tokens;
|
|
205
|
+
}>>;
|
|
206
|
+
Register: ({
|
|
207
|
+
...args
|
|
208
|
+
}: RegisterSchemaType) => Promise<Result<{
|
|
209
|
+
account: Account;
|
|
210
|
+
tokens: Tokens;
|
|
211
|
+
}>>;
|
|
212
|
+
Logout: ({
|
|
213
|
+
...args
|
|
214
|
+
}: LogoutSchemaType) => Promise<Result<any>>;
|
|
215
|
+
Refresh: ({
|
|
216
|
+
...args
|
|
217
|
+
}: RefreshTokenSchemaType) => Promise<Result$1<{
|
|
218
|
+
account: Account;
|
|
219
|
+
tokens: Tokens;
|
|
220
|
+
}>>;
|
|
221
|
+
ChangePassword: ({
|
|
222
|
+
...args
|
|
223
|
+
}: ChangePasswordSchemaType) => Promise<Result<unknown>>;
|
|
224
|
+
RequestOTPCode: ({
|
|
225
|
+
...args
|
|
226
|
+
}: Omit<LoginSchemaType, "password"> & {
|
|
227
|
+
password?: string;
|
|
228
|
+
usePassword?: boolean;
|
|
229
|
+
otpPurpose: OtpPurpose;
|
|
230
|
+
}) => Promise<Result<{
|
|
231
|
+
id: string;
|
|
232
|
+
code: string;
|
|
233
|
+
}>>;
|
|
234
|
+
LoginWithOTP: ({
|
|
235
|
+
...args
|
|
236
|
+
}: Omit<LoginSchemaType, "password"> & {
|
|
237
|
+
code: string;
|
|
238
|
+
}) => Promise<Result<{
|
|
239
|
+
account: Account;
|
|
240
|
+
tokens: Tokens;
|
|
241
|
+
}>>;
|
|
242
|
+
VerifyOTP: ({
|
|
243
|
+
...args
|
|
244
|
+
}: {
|
|
245
|
+
id: string;
|
|
246
|
+
code: string;
|
|
247
|
+
otpPurpose: OtpPurpose;
|
|
248
|
+
}) => Promise<{
|
|
249
|
+
isValid: boolean;
|
|
250
|
+
}>;
|
|
251
|
+
};
|
|
252
|
+
Tokens: {
|
|
253
|
+
GenerateRefreshToken: (payload: any) => Promise<string>;
|
|
254
|
+
GenerateAccessToken: (payload: any) => Promise<string>;
|
|
255
|
+
GenerateTokenPairs: (payload: any) => Promise<{
|
|
256
|
+
accessToken: string;
|
|
257
|
+
refreshToken: string;
|
|
258
|
+
}>;
|
|
259
|
+
VerifyRefreshToken: <T_1>(token: any) => Promise<T_1 | null>;
|
|
260
|
+
VerifyAccessToken: <T_1>(token: any) => Promise<T_1 | null>;
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
//#endregion
|
|
264
|
+
//#region ../core/src/types/routes.contract.t.d.ts
|
|
265
|
+
type RouteDeps = {
|
|
266
|
+
config: CAuthOptions;
|
|
267
|
+
tokens: _CAuth<any>['Tokens'];
|
|
268
|
+
};
|
|
269
|
+
type AuthGuardDeps = {
|
|
270
|
+
config: CAuthOptions;
|
|
271
|
+
tokens: _CAuth<any>['Tokens'];
|
|
272
|
+
roles?: Array<string>;
|
|
273
|
+
};
|
|
274
|
+
interface RoutesContract {
|
|
275
|
+
Login({
|
|
276
|
+
...config
|
|
277
|
+
}: RouteDeps): any;
|
|
278
|
+
Register({
|
|
279
|
+
...config
|
|
280
|
+
}: RouteDeps): any;
|
|
281
|
+
Logout({
|
|
282
|
+
...config
|
|
283
|
+
}: RouteDeps): any;
|
|
284
|
+
Guard({
|
|
285
|
+
...config
|
|
286
|
+
}: AuthGuardDeps): any;
|
|
287
|
+
Refresh({
|
|
288
|
+
...config
|
|
289
|
+
}: AuthGuardDeps): any;
|
|
290
|
+
ChangePassword({
|
|
291
|
+
...config
|
|
292
|
+
}: RouteDeps & {
|
|
293
|
+
userId: string;
|
|
294
|
+
}): any;
|
|
295
|
+
}
|
|
296
|
+
//#endregion
|
|
297
|
+
//#region src/express.contractor.d.ts
|
|
298
|
+
declare class ExpressContractor implements RoutesContract {
|
|
299
|
+
Register: ({
|
|
300
|
+
config,
|
|
301
|
+
tokens
|
|
302
|
+
}: RouteDeps) => (req: express0.Request, res: express0.Response) => Promise<express0.Response<any, Record<string, any>>>;
|
|
303
|
+
Login: ({
|
|
304
|
+
config,
|
|
305
|
+
tokens
|
|
306
|
+
}: RouteDeps) => (req: express0.Request, res: express0.Response) => Promise<express0.Response<any, Record<string, any>>>;
|
|
307
|
+
Logout: ({
|
|
308
|
+
config,
|
|
309
|
+
tokens
|
|
310
|
+
}: RouteDeps) => (req: express0.Request, res: express0.Response) => Promise<express0.Response<any, Record<string, any>>>;
|
|
311
|
+
Refresh: ({
|
|
312
|
+
config,
|
|
313
|
+
tokens
|
|
314
|
+
}: RouteDeps) => (req: express0.Request, res: express0.Response) => Promise<express0.Response<any, Record<string, any>>>;
|
|
315
|
+
ChangePassword: ({
|
|
316
|
+
config,
|
|
317
|
+
tokens,
|
|
318
|
+
userId
|
|
319
|
+
}: RouteDeps & {
|
|
320
|
+
userId: string;
|
|
321
|
+
}) => (req: express0.Request, res: express0.Response) => Promise<express0.Response<any, Record<string, any>>>;
|
|
322
|
+
Guard: ({
|
|
323
|
+
config,
|
|
324
|
+
tokens,
|
|
325
|
+
roles
|
|
326
|
+
}: AuthGuardDeps) => (req: express0.Request, res: express0.Response, next: express0.NextFunction) => Promise<void | express0.Response<any, Record<string, any>>>;
|
|
327
|
+
}
|
|
328
|
+
//#endregion
|
|
329
|
+
//#region src/types/express.d.ts
|
|
330
|
+
declare global {
|
|
331
|
+
namespace Express {
|
|
332
|
+
interface Request {
|
|
333
|
+
cauth?: {
|
|
334
|
+
id: string;
|
|
335
|
+
role: string;
|
|
336
|
+
};
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
//#endregion
|
|
341
|
+
export { ExpressContractor };
|
package/package.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@cauth/express",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "./dist/index.cjs",
|
|
6
|
+
"types": "./dist/index.d.cts",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"build": "tsdown",
|
|
10
|
+
"patch": "npm version patch",
|
|
11
|
+
"minor": "npm version minor",
|
|
12
|
+
"major": "npm version major",
|
|
13
|
+
"publish": "npm publish"
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist/",
|
|
17
|
+
"README.md",
|
|
18
|
+
"package.json"
|
|
19
|
+
],
|
|
20
|
+
"keywords": [
|
|
21
|
+
"authentication",
|
|
22
|
+
"node-auth",
|
|
23
|
+
"express auth",
|
|
24
|
+
"easy-auth"
|
|
25
|
+
],
|
|
26
|
+
"author": "Jonace Mpelule <jonacempelule123@gmail.com> (https://github.com/jonace-mpelule)",
|
|
27
|
+
"license": "MIT",
|
|
28
|
+
"packageManager": "pnpm@10.13.1",
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"@types/bcrypt": "^6.0.0",
|
|
31
|
+
"@types/express": "^5.0.3",
|
|
32
|
+
"@types/node": "^24.7.0",
|
|
33
|
+
"tsdown": "^0.15.6",
|
|
34
|
+
"typescript": "^5.9.3"
|
|
35
|
+
},
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"bcrypt": "^6.0.0",
|
|
38
|
+
"express": "^5.1.0"
|
|
39
|
+
},
|
|
40
|
+
"engines": {
|
|
41
|
+
"node": ">=18"
|
|
42
|
+
}
|
|
43
|
+
}
|