fiber-firebase-functions 1.0.2 → 1.0.4

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.
Files changed (43) hide show
  1. package/README.md +74 -0
  2. package/lib/auth/is_user_disabled.js +37 -36
  3. package/lib/auth/is_user_disabled.js.map +1 -1
  4. package/lib/auth/is_user_exists.js +31 -30
  5. package/lib/auth/is_user_exists.js.map +1 -1
  6. package/lib/auth/otp.js +162 -0
  7. package/lib/auth/otp.js.map +1 -0
  8. package/lib/auth/reset_password.js +327 -0
  9. package/lib/auth/reset_password.js.map +1 -0
  10. package/lib/auth/update_password.js +18 -7
  11. package/lib/auth/update_password.js.map +1 -1
  12. package/lib/auth/user.js +44 -32
  13. package/lib/auth/user.js.map +1 -1
  14. package/lib/common/config.js +64 -0
  15. package/lib/common/config.js.map +1 -0
  16. package/lib/common/locale.js +119 -0
  17. package/lib/common/locale.js.map +1 -0
  18. package/lib/email/email.js +96 -0
  19. package/lib/email/email.js.map +1 -0
  20. package/lib/email/send_email.js +81 -0
  21. package/lib/email/send_email.js.map +1 -0
  22. package/lib/email/templates/new_user.js +491 -0
  23. package/lib/email/templates/new_user.js.map +1 -0
  24. package/lib/email/templates.js +38 -0
  25. package/lib/email/templates.js.map +1 -0
  26. package/lib/index.js +6 -0
  27. package/lib/index.js.map +1 -1
  28. package/lib/middleware/rate_limiter.js +19 -6
  29. package/lib/middleware/rate_limiter.js.map +1 -1
  30. package/package.json +6 -4
  31. package/src/auth/is_user_disabled.ts +31 -29
  32. package/src/auth/is_user_exists.ts +25 -23
  33. package/src/auth/otp.ts +135 -0
  34. package/src/auth/reset_password.ts +317 -0
  35. package/src/auth/user.ts +34 -24
  36. package/src/common/config.ts +84 -0
  37. package/src/common/locale.ts +121 -0
  38. package/src/email/email.ts +70 -0
  39. package/src/email/templates/new_user.ts +493 -0
  40. package/src/email/templates.ts +34 -0
  41. package/src/index.ts +6 -0
  42. package/src/middleware/rate_limiter.ts +25 -6
  43. package/src/auth/update_password.ts +0 -211
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fiber-firebase-functions",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "description": "A collection of ready-to-use Firebase Cloud Functions utilities and wrappers designed for any application built by Fiber. Provides reusable helpers, common patterns, and production-grade modules to streamline backend development across all Fiber projects.",
5
5
  "author": "Fiber",
6
6
  "license": "FIBER-PROPRIETARY",
@@ -20,10 +20,12 @@
20
20
  ],
21
21
  "dependencies": {
22
22
  "firebase-admin": "^13.6.0",
23
- "firebase-functions": "^7.0.0"
23
+ "firebase-functions": "^7.0.0",
24
+ "validator": "^13.15.23"
24
25
  },
25
26
  "devDependencies": {
26
- "typescript": "^5.9.3",
27
- "ts-node": "^10.9.2"
27
+ "@types/validator": "^13.15.10",
28
+ "ts-node": "^10.9.2",
29
+ "typescript": "^5.9.3"
28
30
  }
29
31
  }
@@ -39,7 +39,7 @@ export enum UserDisabledByIdStatus {
39
39
  MISSING_USER_ID = "MISSING_USER_ID",
40
40
  ENABLED = "ENABLED",
41
41
  DISABLED = "DISABLED",
42
- NOT_FOUND = "NOT_FOUND",
42
+ USER_NOT_FOUND = "USER_NOT_FOUND",
43
43
  INTERNAL_ERROR = "INTERNAL_ERROR",
44
44
  }
45
45
 
@@ -47,46 +47,48 @@ export enum UserDisabledByEmailStatus {
47
47
  MISSING_USER_ID = "MISSING_USER_ID",
48
48
  ENABLED = "ENABLED",
49
49
  DISABLED = "DISABLED",
50
- NOT_FOUND = "NOT_FOUND",
50
+ USER_NOT_FOUND = "USER_NOT_FOUND",
51
51
  INTERNAL_ERROR = "INTERNAL_ERROR",
52
52
  }
53
53
 
54
- export async function isUserDisabledById(userId: string): Promise<UserDisabledByIdStatus> {
55
- userId = userId.trim();
54
+ export class IsUserDisabled {
55
+ static async withId(userId: string): Promise<UserDisabledByIdStatus> {
56
+ userId = userId.trim();
56
57
 
57
- if (!userId) return UserDisabledByIdStatus.MISSING_USER_ID;
58
+ if (!userId || userId === "") return UserDisabledByIdStatus.MISSING_USER_ID;
58
59
 
59
- try {
60
- const user = await admin.auth().getUser(userId);
61
- const isUserDisabled = user.disabled;
60
+ try {
61
+ const user = await admin.auth().getUser(userId);
62
+ const isUserDisabled = user.disabled;
62
63
 
63
- return isUserDisabled
64
- ? UserDisabledByIdStatus.DISABLED
65
- : UserDisabledByIdStatus.ENABLED;
66
- } catch (error: any) {
67
- if (error.code === "auth/user-not-found") {
68
- return UserDisabledByIdStatus.NOT_FOUND;
64
+ return isUserDisabled
65
+ ? UserDisabledByIdStatus.DISABLED
66
+ : UserDisabledByIdStatus.ENABLED;
67
+ } catch (error: any) {
68
+ if (error.code === "auth/user-not-found") {
69
+ return UserDisabledByIdStatus.USER_NOT_FOUND;
70
+ }
71
+ return UserDisabledByIdStatus.INTERNAL_ERROR;
69
72
  }
70
- return UserDisabledByIdStatus.INTERNAL_ERROR;
71
73
  }
72
- }
73
74
 
74
- export async function isUserDisabledByEmail(email: string): Promise<UserDisabledByEmailStatus> {
75
- email = email.trim();
75
+ static async withEmail(email: string): Promise<UserDisabledByEmailStatus> {
76
+ email = email.trim();
76
77
 
77
- if (!email) return UserDisabledByEmailStatus.MISSING_USER_ID;
78
+ if (!email || email === "") return UserDisabledByEmailStatus.MISSING_USER_ID;
78
79
 
79
- try {
80
- const user = await admin.auth().getUserByEmail(email);
81
- const isUserDisabled = user.disabled;
80
+ try {
81
+ const user = await admin.auth().getUserByEmail(email);
82
+ const isUserDisabled = user.disabled;
82
83
 
83
- return isUserDisabled
84
- ? UserDisabledByEmailStatus.DISABLED
85
- : UserDisabledByEmailStatus.ENABLED;
86
- } catch (error: any) {
87
- if (error.code === "auth/user-not-found") {
88
- return UserDisabledByEmailStatus.NOT_FOUND;
84
+ return isUserDisabled
85
+ ? UserDisabledByEmailStatus.DISABLED
86
+ : UserDisabledByEmailStatus.ENABLED;
87
+ } catch (error: any) {
88
+ if (error.code === "auth/user-not-found") {
89
+ return UserDisabledByEmailStatus.USER_NOT_FOUND;
90
+ }
91
+ return UserDisabledByEmailStatus.INTERNAL_ERROR;
89
92
  }
90
- return UserDisabledByEmailStatus.INTERNAL_ERROR;
91
93
  }
92
94
  }
@@ -38,47 +38,49 @@ if (admin.apps.length === 0) {
38
38
  export enum UserExistsByIdStatus {
39
39
  MISSING_USER_ID = "MISSING_USER_ID",
40
40
  EXISTS = "EXISTS",
41
- NOT_FOUND = "NOT_FOUND",
41
+ USER_NOT_FOUND = "USER_NOT_FOUND",
42
42
  INTERNAL_ERROR = "INTERNAL_ERROR",
43
43
  }
44
44
 
45
45
  export enum UserExistsByEmailStatus {
46
46
  MISSING_USER_EMAIL = "MISSING_USER_EMAIL",
47
47
  EXISTS = "EXISTS",
48
- NOT_FOUND = "NOT_FOUND",
48
+ USER_NOT_FOUND = "USER_NOT_FOUND",
49
49
  INTERNAL_ERROR = "INTERNAL_ERROR",
50
50
  }
51
51
 
52
- export async function isUserExistsById(userId: string): Promise<UserExistsByIdStatus> {
53
- userId = userId.trim();
52
+ export class IsUserExists {
53
+ static async withId(userId: string): Promise<UserExistsByIdStatus> {
54
+ userId = userId.trim();
54
55
 
55
- if (!userId || userId === "") return UserExistsByIdStatus.MISSING_USER_ID;
56
+ if (!userId || userId === "") return UserExistsByIdStatus.MISSING_USER_ID;
56
57
 
57
- try {
58
- await admin.auth().getUser(userId);
58
+ try {
59
+ await admin.auth().getUser(userId);
59
60
 
60
- return UserExistsByIdStatus.EXISTS;
61
- } catch (error: any) {
62
- if (error.code === "auth/user-not-found") {
63
- return UserExistsByIdStatus.NOT_FOUND;
61
+ return UserExistsByIdStatus.EXISTS;
62
+ } catch (error: any) {
63
+ if (error.code === "auth/user-not-found") {
64
+ return UserExistsByIdStatus.USER_NOT_FOUND;
65
+ }
66
+ return UserExistsByIdStatus.INTERNAL_ERROR;
64
67
  }
65
- return UserExistsByIdStatus.INTERNAL_ERROR;
66
68
  }
67
- }
68
69
 
69
- export async function isUserExistsByEmail(email: string): Promise<UserExistsByEmailStatus> {
70
- email = email.trim();
70
+ static async withEmail(email: string): Promise<UserExistsByEmailStatus> {
71
+ email = email.trim();
71
72
 
72
- if (!email || email === "") return UserExistsByEmailStatus.MISSING_USER_EMAIL;
73
+ if (!email || email === "") return UserExistsByEmailStatus.MISSING_USER_EMAIL;
73
74
 
74
- try {
75
- await admin.auth().getUserByEmail(email);
75
+ try {
76
+ await admin.auth().getUserByEmail(email);
76
77
 
77
- return UserExistsByEmailStatus.EXISTS;
78
- } catch (error: any) {
79
- if (error.code === "auth/user-not-found") {
80
- return UserExistsByEmailStatus.NOT_FOUND;
78
+ return UserExistsByEmailStatus.EXISTS;
79
+ } catch (error: any) {
80
+ if (error.code === "auth/user-not-found") {
81
+ return UserExistsByEmailStatus.USER_NOT_FOUND;
82
+ }
83
+ return UserExistsByEmailStatus.INTERNAL_ERROR;
81
84
  }
82
- return UserExistsByEmailStatus.INTERNAL_ERROR;
83
85
  }
84
86
  }
@@ -0,0 +1,135 @@
1
+ /*
2
+ * Copyright (C) 2025 Fiber
3
+ *
4
+ * All rights reserved. This script, including its code and logic, is the
5
+ * exclusive property of Fiber. Redistribution, reproduction,
6
+ * or modification of any part of this script is strictly prohibited
7
+ * without prior written permission from Fiber.
8
+ *
9
+ * Conditions of use:
10
+ * - The code may not be copied, duplicated, or used, in whole or in part,
11
+ * for any purpose without explicit authorization.
12
+ * - Redistribution of this code, with or without modification, is not
13
+ * permitted unless expressly agreed upon by Fiber.
14
+ * - The name "Fiber" and any associated branding, logos, or
15
+ * trademarks may not be used to endorse or promote derived products
16
+ * or services without prior written approval.
17
+ *
18
+ * Disclaimer:
19
+ * THIS SCRIPT AND ITS CODE ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
20
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY,
21
+ * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. IN NO EVENT SHALL
22
+ * FIBER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING BUT NOT LIMITED TO LOSS OF USE,
24
+ * DATA, PROFITS, OR BUSINESS INTERRUPTION) ARISING OUT OF OR RELATED TO THE USE
25
+ * OR INABILITY TO USE THIS SCRIPT, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
+ *
27
+ * Unauthorized copying or reproduction of this script, in whole or in part,
28
+ * is a violation of applicable intellectual property laws and will result
29
+ * in legal action.
30
+ */
31
+
32
+ import crypto from 'crypto';
33
+ import * as admin from "firebase-admin";
34
+ import { appInitialize } from "../common/config";
35
+
36
+ if (admin.apps.length === 0) {
37
+ admin.initializeApp();
38
+ }
39
+
40
+ export enum GenerateStatus {
41
+ MISSING_OTP_CONFIG = "MISSING_OTP_CONFIG",
42
+ SUCCESS = "SUCCESS",
43
+ }
44
+
45
+ export enum GetOTPStatus {
46
+ MISSING_OTP_CONFIG = "MISSING_OTP_CONFIG",
47
+ OTP_NOT_FOUND = "OTP_NOT_FOUND",
48
+ OTP_FOUND = "OTP_FOUND",
49
+ }
50
+
51
+ export class Otp {
52
+ static async generate(userId: string, type: string): Promise<GenerateStatus> {
53
+ const config = appInitialize();
54
+ const otp = config.otp;
55
+
56
+ if (otp.collection === undefined) return GenerateStatus.MISSING_OTP_CONFIG;
57
+
58
+ const now = Date.now();
59
+ const otpCode = OTPUtils.generate();
60
+ const hashOtp = OTPUtils.hash(otpCode);
61
+
62
+ await admin.firestore().collection(otp.collection).add({
63
+ __fbs__user_id: userId,
64
+ __fbs__created_at: now,
65
+ __fbs__otp_type: type,
66
+ __fbs__otp: hashOtp,
67
+ });
68
+
69
+ return GenerateStatus.SUCCESS;
70
+ }
71
+
72
+ static async get(userId: string, type: string): Promise<{ status: GetOTPStatus; otp?: string; }> {
73
+ const config = appInitialize();
74
+ const otp = config.otp;
75
+
76
+ if (otp.collection === undefined) return { status: GetOTPStatus.MISSING_OTP_CONFIG };
77
+
78
+ const snapshot = await admin.firestore()
79
+ .collection(otp.collection)
80
+ .where("__fbs__user_id", "==", userId)
81
+ .where("__fbs__otp_type", "==", type)
82
+ .orderBy("__fbs__created_at", "desc")
83
+ .limit(1)
84
+ .get();
85
+
86
+ if (snapshot.docs.length === 0) return { status: GetOTPStatus.OTP_NOT_FOUND };
87
+
88
+ const doc = snapshot.docs[0];
89
+ const data = doc?.data();
90
+ const otpCode = data?.__fbs__otp as string;
91
+
92
+ return { status: GetOTPStatus.MISSING_OTP_CONFIG, otp: otpCode };
93
+ }
94
+ }
95
+
96
+ class OTPUtils {
97
+ static generate(): string {
98
+ while (true) {
99
+ const buffer = crypto.randomBytes(4);
100
+ const number = buffer.readUInt32BE();
101
+ const otp = (number % 1000000).toString().padStart(6, '0');
102
+
103
+ if (this.hasAllIdenticalDigits(otp)) continue;
104
+ if (this.hasRepeatingPattern(otp)) continue;
105
+ if (this.hasMoreThanThreeConsecutiveIdenticalDigits(otp)) continue;
106
+
107
+ return otp;
108
+ }
109
+ }
110
+
111
+ static hash(otp: string): string {
112
+ return crypto.createHash('sha256').update(otp).digest('hex');
113
+ }
114
+
115
+ private static hasAllIdenticalDigits(str: string): boolean {
116
+ return /^(\d)\1{5}$/.test(str);
117
+ }
118
+
119
+ private static hasMoreThanThreeConsecutiveIdenticalDigits(str: string): boolean {
120
+ return /(\d)\1{3,}/.test(str);
121
+ }
122
+
123
+ private static hasRepeatingPattern(str: string): boolean {
124
+ const half = str.slice(0, 3);
125
+ if (half.repeat(2) === str) return true;
126
+
127
+ const pairs = str.match(/(..)/g);
128
+ if (pairs && pairs.length === 3 && new Set(pairs).size === 1) return true;
129
+
130
+ const mirror = half + half.split('').reverse().join('');
131
+ if (str === mirror) return true;
132
+
133
+ return false;
134
+ }
135
+ }
@@ -0,0 +1,317 @@
1
+ /*
2
+ * Copyright (C) 2025 Fiber
3
+ *
4
+ * All rights reserved. This script, including its code and logic, is the
5
+ * exclusive property of Fiber. Redistribution, reproduction,
6
+ * or modification of any part of this script is strictly prohibited
7
+ * without prior written permission from Fiber.
8
+ *
9
+ * Conditions of use:
10
+ * - The code may not be copied, duplicated, or used, in whole or in part,
11
+ * for any purpose without explicit authorization.
12
+ * - Redistribution of this code, with or without modification, is not
13
+ * permitted unless expressly agreed upon by Fiber.
14
+ * - The name "Fiber" and any associated branding, logos, or
15
+ * trademarks may not be used to endorse or promote derived products
16
+ * or services without prior written approval.
17
+ *
18
+ * Disclaimer:
19
+ * THIS SCRIPT AND ITS CODE ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
20
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY,
21
+ * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. IN NO EVENT SHALL
22
+ * FIBER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING BUT NOT LIMITED TO LOSS OF USE,
24
+ * DATA, PROFITS, OR BUSINESS INTERRUPTION) ARISING OUT OF OR RELATED TO THE USE
25
+ * OR INABILITY TO USE THIS SCRIPT, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
+ *
27
+ * Unauthorized copying or reproduction of this script, in whole or in part,
28
+ * is a violation of applicable intellectual property laws and will result
29
+ * in legal action.
30
+ */
31
+
32
+ import * as admin from "firebase-admin";
33
+ import validator from "validator";
34
+ import { appInitialize } from "../common/config";
35
+ import { isRateLimited, RateLimitCheckStatus, RateLimitIdentifier, RateLimitRule, recordRateLimitHit } from "../middleware/rate_limiter";
36
+ import { IsUserDisabled, UserDisabledByIdStatus } from "./is_user_disabled";
37
+ import { IsUserExists, UserExistsByIdStatus } from "./is_user_exists";
38
+ import { Otp } from "./otp";
39
+ import { User, UserByEmailStatus } from "./user";
40
+
41
+ if (admin.apps.length === 0) {
42
+ admin.initializeApp();
43
+ }
44
+
45
+ export enum ResetPasswordByEmailStatus {
46
+ MISSING_DATABASE_CONFIG = "MISSING_DATABASE_CONFIG",
47
+ MISSING_USER_EMAIL = "MISSING_USER_EMAIL",
48
+ MISSING_NEW_PASSWORD = "MISSING_NEW_PASSWORD",
49
+ MISSING_CONFIRM_NEW_PASSWORD = "MISSING_CONFIRM_NEW_PASSWORD",
50
+ MISSING_PASSWORD_POLICY = "MISSING_PASSWORD_POLICY",
51
+ NOT_IDENTICAL_CONFIRM_PASSWORD = "NOT_IDENTICAL_CONFIRM_PASSWORD",
52
+ USER_NOT_FOUND = "USER_NOT_FOUND",
53
+ USER_DISABLED = "USER_DISABLED",
54
+ WEAK_NEW_PASSWORD = "WEAK_NEW_PASSWORD",
55
+ MISSING_PASSWORD_UPPERCASE = "MISSING_PASSWORD_UPPERCASE",
56
+ MISSING_PASSWORD_LOWERCASE = "MISSING_PASSWORD_LOWERCASE",
57
+ MISSING_PASSWORD_DIGIT = "MISSING_PASSWORD_DIGIT",
58
+ MISSING_PASSWORD_SPECIAL_CHAR = "MISSING_PASSWORD_SPECIAL_CHAR",
59
+ TOO_MANY_REQUEST = "TOO_MANY_REQUEST",
60
+ INVALID_EMAIL_FORMAT = "INVALID_EMAIL_FORMAT",
61
+ SUCCESS = "SUCCESS",
62
+ INTERNAL_ERROR = "INTERNAL_ERROR",
63
+ }
64
+
65
+ export enum ResetPasswordByIdStatus {
66
+ MISSING_DATABASE_CONFIG = "MISSING_DATABASE_CONFIG",
67
+ MISSING_USER_ID = "MISSING_USER_ID",
68
+ MISSING_NEW_PASSWORD = "MISSING_NEW_PASSWORD",
69
+ MISSING_CONFIRM_NEW_PASSWORD = "MISSING_CONFIRM_NEW_PASSWORD",
70
+ MISSING_PASSWORD_POLICY = "MISSING_PASSWORD_POLICY",
71
+ NOT_IDENTICAL_CONFIRM_PASSWORD = "NOT_IDENTICAL_CONFIRM_PASSWORD",
72
+ USER_NOT_FOUND = "USER_NOT_FOUND",
73
+ USER_DISABLED = "USER_DISABLED",
74
+ WEAK_NEW_PASSWORD = "WEAK_NEW_PASSWORD",
75
+ MISSING_PASSWORD_UPPERCASE = "MISSING_PASSWORD_UPPERCASE",
76
+ MISSING_PASSWORD_LOWERCASE = "MISSING_PASSWORD_LOWERCASE",
77
+ MISSING_PASSWORD_DIGIT = "MISSING_PASSWORD_DIGIT",
78
+ MISSING_PASSWORD_SPECIAL_CHAR = "MISSING_PASSWORD_SPECIAL_CHAR",
79
+ TOO_MANY_REQUEST = "TOO_MANY_REQUEST",
80
+ SUCCESS = "SUCCESS",
81
+ INTERNAL_ERROR = "INTERNAL_ERROR",
82
+ }
83
+
84
+ export enum RequestResetPasswordByIdStatus {
85
+ MISSING_OTP_CONFIG = "MISSING_OTP_CONFIG",
86
+ MISSING_DATABASE_CONFIG = "MISSING_DATABASE_CONFIG",
87
+ MISSING_USER_ID = "MISSING_USER_ID",
88
+ TOO_MANY_REQUEST = "TOO_MANY_REQUEST",
89
+ USER_NOT_FOUND = "USER_NOT_FOUND",
90
+ USER_DISABLED = "USER_DISABLED",
91
+ SUCCESS = "SUCCESS",
92
+ INTERNAL_ERROR = "INTERNAL_ERROR",
93
+ }
94
+
95
+ export enum RequestResetPasswordByEmailStatus {
96
+ MISSING_OTP_CONFIG = "MISSING_OTP_CONFIG",
97
+ MISSING_DATABASE_CONFIG = "MISSING_DATABASE_CONFIG",
98
+ MISSING_USER_EMAIL = "MISSING_USER_EMAIL",
99
+ TOO_MANY_REQUEST = "TOO_MANY_REQUEST",
100
+ USER_NOT_FOUND = "USER_NOT_FOUND",
101
+ USER_DISABLED = "USER_DISABLED",
102
+ INVALID_EMAIL_FORMAT = "INVALID_EMAIL_FORMAT",
103
+ SUCCESS = "SUCCESS",
104
+ INTERNAL_ERROR = "INTERNAL_ERROR",
105
+ }
106
+
107
+ export interface PasswordPolicy {
108
+ minLength: number;
109
+ requireUppercase: boolean;
110
+ requireLowercase: boolean;
111
+ requireDigit: boolean;
112
+ requireSpecial: boolean;
113
+ }
114
+
115
+ export interface ResetPassword {
116
+ newPassword: string;
117
+ confirmNewPassword: string;
118
+ passwordPolicy: PasswordPolicy;
119
+ }
120
+
121
+ export class RequestResetPassword {
122
+ static async withId(userId: string): Promise<RequestResetPasswordByIdStatus> {
123
+ const config = appInitialize();
124
+ const otp = config.otp;
125
+ const rateLimiter = config.rateLimiter;
126
+
127
+ if (otp.collection === undefined) return RequestResetPasswordByIdStatus.MISSING_OTP_CONFIG;
128
+ if (rateLimiter.appName === undefined || rateLimiter.url === undefined) {
129
+ return RequestResetPasswordByIdStatus.MISSING_DATABASE_CONFIG;
130
+ }
131
+
132
+ userId = userId.trim();
133
+ if (!userId || userId === "") return RequestResetPasswordByIdStatus.MISSING_USER_ID;
134
+
135
+ const identifier: RateLimitIdentifier = {
136
+ id: userId,
137
+ target: "request_reset_password"
138
+ };
139
+
140
+ const rule: RateLimitRule = {
141
+ ttl: 2 * 60 * 1000,
142
+ windowMs: 3 * 60 * 1000,
143
+ maxHits: 5,
144
+ };
145
+
146
+ const userExists = await IsUserExists.withId(userId);
147
+ if (userExists === UserExistsByIdStatus.MISSING_USER_ID) return RequestResetPasswordByIdStatus.MISSING_USER_ID;
148
+ if (userExists === UserExistsByIdStatus.INTERNAL_ERROR) return RequestResetPasswordByIdStatus.INTERNAL_ERROR;
149
+ if (userExists === UserExistsByIdStatus.USER_NOT_FOUND) return RequestResetPasswordByIdStatus.USER_NOT_FOUND;
150
+
151
+ const userDisabled = await IsUserDisabled.withId(userId);
152
+ if (userDisabled === UserDisabledByIdStatus.MISSING_USER_ID) {
153
+ return RequestResetPasswordByIdStatus.MISSING_USER_ID;
154
+ }
155
+ if (userDisabled === UserDisabledByIdStatus.INTERNAL_ERROR) {
156
+ return RequestResetPasswordByIdStatus.INTERNAL_ERROR;
157
+ }
158
+ if (userDisabled === UserDisabledByIdStatus.USER_NOT_FOUND) {
159
+ return RequestResetPasswordByIdStatus.USER_NOT_FOUND;
160
+ }
161
+
162
+ if (await isRateLimited(identifier, rule) !== RateLimitCheckStatus.LIMIT_NOT_FOUND) {
163
+ return RequestResetPasswordByIdStatus.TOO_MANY_REQUEST;
164
+ }
165
+ await recordRateLimitHit(identifier, rule);
166
+
167
+ if (await IsUserExists.withId(userId)) return RequestResetPasswordByIdStatus.USER_NOT_FOUND;
168
+ if (await IsUserDisabled.withId(userId)) return RequestResetPasswordByIdStatus.USER_DISABLED;
169
+
170
+ await Otp.generate(userId, "request_reset_password");
171
+
172
+ return RequestResetPasswordByIdStatus.SUCCESS;
173
+ }
174
+
175
+ static async withEmail(email: string): Promise<RequestResetPasswordByEmailStatus> {
176
+ email = email.trim();
177
+ if (!email || email === "") return RequestResetPasswordByEmailStatus.MISSING_USER_EMAIL;
178
+
179
+ if (!validator.isEmail(email)) return RequestResetPasswordByEmailStatus.INVALID_EMAIL_FORMAT;
180
+
181
+ const user = await User.withEmail(email);
182
+ if (user.status === UserByEmailStatus.INTERNAL_ERROR) return RequestResetPasswordByEmailStatus.INTERNAL_ERROR;
183
+ if (user.status === UserByEmailStatus.MISSING_EMAIL) {
184
+ return RequestResetPasswordByEmailStatus.MISSING_USER_EMAIL;
185
+ }
186
+ if (user.status === UserByEmailStatus.USER_NOT_FOUND) return RequestResetPasswordByEmailStatus.USER_NOT_FOUND;
187
+
188
+ const userId = user.user?.uid;
189
+ if (!userId || userId === undefined) return RequestResetPasswordByEmailStatus.INTERNAL_ERROR;
190
+
191
+ const result = await this.withId(userId);
192
+
193
+ const map = {
194
+ [RequestResetPasswordByIdStatus.MISSING_OTP_CONFIG]: RequestResetPasswordByEmailStatus.MISSING_OTP_CONFIG,
195
+ [RequestResetPasswordByIdStatus.MISSING_DATABASE_CONFIG]: RequestResetPasswordByEmailStatus.MISSING_DATABASE_CONFIG,
196
+ [RequestResetPasswordByIdStatus.MISSING_USER_ID]: RequestResetPasswordByEmailStatus.MISSING_USER_EMAIL,
197
+ [RequestResetPasswordByIdStatus.TOO_MANY_REQUEST]: RequestResetPasswordByEmailStatus.TOO_MANY_REQUEST,
198
+ [RequestResetPasswordByIdStatus.USER_NOT_FOUND]: RequestResetPasswordByEmailStatus.USER_NOT_FOUND,
199
+ [RequestResetPasswordByIdStatus.USER_DISABLED]: RequestResetPasswordByEmailStatus.USER_DISABLED,
200
+ [RequestResetPasswordByIdStatus.SUCCESS]: RequestResetPasswordByEmailStatus.SUCCESS,
201
+ [RequestResetPasswordByIdStatus.INTERNAL_ERROR]: RequestResetPasswordByEmailStatus.INTERNAL_ERROR,
202
+ };
203
+
204
+ return map[result];
205
+ }
206
+ }
207
+
208
+ export class VerifyRequestResetPasswordOTP {
209
+
210
+ }
211
+
212
+ export class ResetPassword {
213
+ static async withId(userId: string, password: ResetPassword): Promise<ResetPasswordByIdStatus> {
214
+ const config = appInitialize();
215
+ const rateLimiter = config.rateLimiter;
216
+
217
+ if (rateLimiter.appName === undefined || rateLimiter.url === undefined) {
218
+ return ResetPasswordByIdStatus.MISSING_DATABASE_CONFIG;
219
+ }
220
+
221
+ userId = userId.trim();
222
+
223
+ if (!userId || userId === "") return ResetPasswordByIdStatus.MISSING_USER_ID;
224
+
225
+ const newPassword = password.newPassword.trim();
226
+ const confirmNewPassword = password.confirmNewPassword.trim();
227
+
228
+ if (!newPassword || newPassword === "") return ResetPasswordByIdStatus.MISSING_NEW_PASSWORD;
229
+ if (!confirmNewPassword || confirmNewPassword === "") return ResetPasswordByIdStatus.MISSING_CONFIRM_NEW_PASSWORD;
230
+
231
+ const passwordPolicy = password.passwordPolicy;
232
+
233
+ if (!passwordPolicy) return ResetPasswordByIdStatus.MISSING_PASSWORD_POLICY;
234
+
235
+ const identifier: RateLimitIdentifier = {
236
+ id: userId,
237
+ target: "reset_password"
238
+ };
239
+
240
+ const rule: RateLimitRule = {
241
+ ttl: 2 * 60 * 1000,
242
+ windowMs: 3 * 60 * 1000,
243
+ maxHits: 5,
244
+ };
245
+
246
+ if (await isRateLimited(identifier, rule) !== RateLimitCheckStatus.LIMIT_NOT_FOUND) {
247
+ return ResetPasswordByIdStatus.TOO_MANY_REQUEST;
248
+ }
249
+ await recordRateLimitHit(identifier, rule);
250
+
251
+ if (await IsUserExists.withId(userId)) return ResetPasswordByIdStatus.USER_NOT_FOUND;
252
+ if (await IsUserDisabled.withId(userId)) return ResetPasswordByIdStatus.USER_DISABLED;
253
+
254
+ if (newPassword !== confirmNewPassword) return ResetPasswordByIdStatus.NOT_IDENTICAL_CONFIRM_PASSWORD;
255
+
256
+ const requiredMin = Math.max(6, passwordPolicy.minLength);
257
+ if (newPassword.length < requiredMin) return ResetPasswordByIdStatus.WEAK_NEW_PASSWORD;
258
+
259
+ const rules = [
260
+ { enabled: passwordPolicy.requireUppercase, regex: /[A-Z]/, error: ResetPasswordByIdStatus.MISSING_PASSWORD_UPPERCASE },
261
+ { enabled: passwordPolicy.requireLowercase, regex: /[a-z]/, error: ResetPasswordByIdStatus.MISSING_PASSWORD_LOWERCASE },
262
+ { enabled: passwordPolicy.requireDigit, regex: /[0-9]/, error: ResetPasswordByIdStatus.MISSING_PASSWORD_DIGIT },
263
+ { enabled: passwordPolicy.requireSpecial, regex: /[^A-Za-z0-9]/, error: ResetPasswordByIdStatus.MISSING_PASSWORD_SPECIAL_CHAR },
264
+ ];
265
+
266
+ for (const rule of rules) {
267
+ if (rule.enabled && !rule.regex.test(newPassword)) return rule.error;
268
+ }
269
+
270
+ try {
271
+ await admin.auth().updateUser(userId, { password: newPassword });
272
+ return ResetPasswordByIdStatus.SUCCESS;
273
+ } catch (error: any) {
274
+ return ResetPasswordByIdStatus.INTERNAL_ERROR;
275
+ }
276
+ }
277
+
278
+ static async withEmail(email: string, password: ResetPassword): Promise<ResetPasswordByEmailStatus> {
279
+ email = email.trim();
280
+ if (!email || email === "") return ResetPasswordByEmailStatus.MISSING_USER_EMAIL;
281
+
282
+ if (!validator.isEmail(email)) return ResetPasswordByEmailStatus.INVALID_EMAIL_FORMAT;
283
+
284
+ const user = await User.withEmail(email);
285
+ if (user.status === UserByEmailStatus.INTERNAL_ERROR) return ResetPasswordByEmailStatus.INTERNAL_ERROR;
286
+ if (user.status === UserByEmailStatus.MISSING_EMAIL) {
287
+ return ResetPasswordByEmailStatus.MISSING_USER_EMAIL;
288
+ }
289
+ if (user.status === UserByEmailStatus.USER_NOT_FOUND) return ResetPasswordByEmailStatus.USER_NOT_FOUND;
290
+
291
+ const userId = user.user?.uid;
292
+ if (!userId || userId === undefined) return ResetPasswordByEmailStatus.INTERNAL_ERROR;
293
+
294
+ const result = await this.withId(userId, password);
295
+
296
+ const map = {
297
+ [ResetPasswordByIdStatus.MISSING_DATABASE_CONFIG]: ResetPasswordByEmailStatus.MISSING_DATABASE_CONFIG,
298
+ [ResetPasswordByIdStatus.MISSING_USER_ID]: ResetPasswordByEmailStatus.MISSING_USER_EMAIL,
299
+ [ResetPasswordByIdStatus.MISSING_NEW_PASSWORD]: ResetPasswordByEmailStatus.MISSING_NEW_PASSWORD,
300
+ [ResetPasswordByIdStatus.MISSING_CONFIRM_NEW_PASSWORD]: ResetPasswordByEmailStatus.MISSING_CONFIRM_NEW_PASSWORD,
301
+ [ResetPasswordByIdStatus.MISSING_PASSWORD_POLICY]: ResetPasswordByEmailStatus.MISSING_PASSWORD_POLICY,
302
+ [ResetPasswordByIdStatus.NOT_IDENTICAL_CONFIRM_PASSWORD]: ResetPasswordByEmailStatus.NOT_IDENTICAL_CONFIRM_PASSWORD,
303
+ [ResetPasswordByIdStatus.USER_NOT_FOUND]: ResetPasswordByEmailStatus.USER_NOT_FOUND,
304
+ [ResetPasswordByIdStatus.USER_DISABLED]: ResetPasswordByEmailStatus.USER_DISABLED,
305
+ [ResetPasswordByIdStatus.WEAK_NEW_PASSWORD]: ResetPasswordByEmailStatus.WEAK_NEW_PASSWORD,
306
+ [ResetPasswordByIdStatus.MISSING_PASSWORD_UPPERCASE]: ResetPasswordByEmailStatus.MISSING_PASSWORD_UPPERCASE,
307
+ [ResetPasswordByIdStatus.MISSING_PASSWORD_LOWERCASE]: ResetPasswordByEmailStatus.MISSING_PASSWORD_LOWERCASE,
308
+ [ResetPasswordByIdStatus.MISSING_PASSWORD_DIGIT]: ResetPasswordByEmailStatus.MISSING_PASSWORD_DIGIT,
309
+ [ResetPasswordByIdStatus.MISSING_PASSWORD_SPECIAL_CHAR]: ResetPasswordByEmailStatus.MISSING_PASSWORD_SPECIAL_CHAR,
310
+ [ResetPasswordByIdStatus.TOO_MANY_REQUEST]: ResetPasswordByEmailStatus.TOO_MANY_REQUEST,
311
+ [ResetPasswordByIdStatus.SUCCESS]: ResetPasswordByEmailStatus.SUCCESS,
312
+ [ResetPasswordByIdStatus.INTERNAL_ERROR]: ResetPasswordByEmailStatus.INTERNAL_ERROR,
313
+ };
314
+
315
+ return map[result];
316
+ }
317
+ }