powr-sdk-api 4.21.0 → 4.21.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.
|
@@ -4,6 +4,13 @@ const jwt = require('jsonwebtoken');
|
|
|
4
4
|
const {
|
|
5
5
|
config
|
|
6
6
|
} = require('../config');
|
|
7
|
+
function tenDigitMobile(raw) {
|
|
8
|
+
const digits = String(raw || "").replace(/\D/g, "");
|
|
9
|
+
if (digits.length === 10) return digits;
|
|
10
|
+
if (digits.length === 12 && digits.startsWith("91")) return digits.slice(2);
|
|
11
|
+
if (digits.length > 10) return digits.slice(-10);
|
|
12
|
+
return digits;
|
|
13
|
+
}
|
|
7
14
|
const generateJWTToken = (user, profile = null, extra = null) => {
|
|
8
15
|
const payload = {
|
|
9
16
|
userId: user._id,
|
|
@@ -11,6 +18,8 @@ const generateJWTToken = (user, profile = null, extra = null) => {
|
|
|
11
18
|
access: profile === null || profile === void 0 ? void 0 : profile.access
|
|
12
19
|
};
|
|
13
20
|
if (user && user.phoneNumber) payload.phoneNumber = String(user.phoneNumber);
|
|
21
|
+
const mobile = tenDigitMobile(user && user.phoneNumber);
|
|
22
|
+
if (mobile.length === 10) payload.mobile = mobile;
|
|
14
23
|
if (extra && extra.impersonatedBy) {
|
|
15
24
|
payload.impersonatedBy = String(extra.impersonatedBy);
|
|
16
25
|
}
|
|
@@ -18,6 +27,16 @@ const generateJWTToken = (user, profile = null, extra = null) => {
|
|
|
18
27
|
expiresIn: '24h'
|
|
19
28
|
});
|
|
20
29
|
};
|
|
30
|
+
const generatePhoneProofToken = mobile => {
|
|
31
|
+
const digits = tenDigitMobile(mobile);
|
|
32
|
+
const payload = {
|
|
33
|
+
mobile: digits
|
|
34
|
+
};
|
|
35
|
+
if (digits.length === 10) payload.phoneNumber = `+91${digits}`;
|
|
36
|
+
return jwt.sign(payload, config.jwtToken, {
|
|
37
|
+
expiresIn: "24h"
|
|
38
|
+
});
|
|
39
|
+
};
|
|
21
40
|
const generateRefreshToken = user => {
|
|
22
41
|
return jwt.sign({
|
|
23
42
|
userId: user._id,
|
|
@@ -58,6 +77,7 @@ const verifyToken = async (req, res, next) => {
|
|
|
58
77
|
powrId: decoded.userId,
|
|
59
78
|
fullName: decoded.fullName,
|
|
60
79
|
access: decoded.access,
|
|
80
|
+
phoneNumber: decoded.phoneNumber || decoded.phone_number || null,
|
|
61
81
|
impersonatedBy: decoded.impersonatedBy || null
|
|
62
82
|
};
|
|
63
83
|
return next();
|
|
@@ -71,6 +91,7 @@ const verifyToken = async (req, res, next) => {
|
|
|
71
91
|
};
|
|
72
92
|
module.exports = {
|
|
73
93
|
generateJWTToken,
|
|
94
|
+
generatePhoneProofToken,
|
|
74
95
|
generateRefreshToken,
|
|
75
96
|
verifyRefreshToken,
|
|
76
97
|
verifyToken
|
package/dist/routes/auth.js
CHANGED
|
@@ -11,6 +11,7 @@ const {
|
|
|
11
11
|
const bcrypt = require("bcrypt");
|
|
12
12
|
const {
|
|
13
13
|
generateJWTToken,
|
|
14
|
+
generatePhoneProofToken,
|
|
14
15
|
generateRefreshToken,
|
|
15
16
|
verifyRefreshToken,
|
|
16
17
|
verifyToken
|
|
@@ -876,9 +877,12 @@ router.post("/verify-otp", async (req, res) => {
|
|
|
876
877
|
});
|
|
877
878
|
const user = q ? await db.collection("users").findOne(q) : null;
|
|
878
879
|
if (!user) {
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
880
|
+
const token = generatePhoneProofToken(mobile);
|
|
881
|
+
return res.status(200).json({
|
|
882
|
+
success: true,
|
|
883
|
+
message: "OTP verified successfully.",
|
|
884
|
+
token,
|
|
885
|
+
access_token: token
|
|
882
886
|
});
|
|
883
887
|
}
|
|
884
888
|
const {
|
|
@@ -891,7 +895,8 @@ router.post("/verify-otp", async (req, res) => {
|
|
|
891
895
|
message: "OTP verified successfully.",
|
|
892
896
|
user: publicUser(user, profile),
|
|
893
897
|
token,
|
|
894
|
-
refreshToken
|
|
898
|
+
refreshToken,
|
|
899
|
+
access_token: token
|
|
895
900
|
});
|
|
896
901
|
} catch (error) {
|
|
897
902
|
console.error("Error in verify-otp:", error);
|
|
@@ -155,12 +155,52 @@ describe("auth OTP routes", () => {
|
|
|
155
155
|
expect(body.success).toBe(true);
|
|
156
156
|
expect(body.token).toEqual(expect.any(String));
|
|
157
157
|
expect(body.refreshToken).toEqual(expect.any(String));
|
|
158
|
-
expect(body.access_token).
|
|
158
|
+
expect(body.access_token).toBe(body.token);
|
|
159
159
|
expect(body.user.fullName).toBe("Test User");
|
|
160
160
|
const jwt = require("jsonwebtoken");
|
|
161
161
|
const decoded = jwt.decode(body.token);
|
|
162
162
|
expect(decoded.userId).toBe(String(userId));
|
|
163
163
|
expect(decoded.fullName).toBe("Test User");
|
|
164
|
-
expect(decoded.mobile).
|
|
164
|
+
expect(decoded.mobile).toBe("7903658469");
|
|
165
|
+
expect(decoded.phoneNumber).toBe("+917903658469");
|
|
166
|
+
});
|
|
167
|
+
it("verify-otp still returns a phone-proof token when there is no Powr user", async () => {
|
|
168
|
+
otp.findLatestOtp.mockResolvedValue({
|
|
169
|
+
_id: "otp2",
|
|
170
|
+
mobile: "7903658469",
|
|
171
|
+
otp: "12345",
|
|
172
|
+
app: "powr",
|
|
173
|
+
verified: false,
|
|
174
|
+
verifyAttempts: 0,
|
|
175
|
+
expiresAt: new Date(Date.now() + 60 * 1000)
|
|
176
|
+
});
|
|
177
|
+
getDb.mockResolvedValue({
|
|
178
|
+
collection(name) {
|
|
179
|
+
if (name === "otps") return {
|
|
180
|
+
updateOne: jest.fn()
|
|
181
|
+
};
|
|
182
|
+
if (name === "users") return {
|
|
183
|
+
findOne: async () => null
|
|
184
|
+
};
|
|
185
|
+
throw new Error(name);
|
|
186
|
+
}
|
|
187
|
+
});
|
|
188
|
+
const req = {
|
|
189
|
+
body: {
|
|
190
|
+
mobile: "7903658469",
|
|
191
|
+
otp: "12345"
|
|
192
|
+
},
|
|
193
|
+
headers: {}
|
|
194
|
+
};
|
|
195
|
+
const res = mockRes();
|
|
196
|
+
await findRoute("post", "/verify-otp")(req, res);
|
|
197
|
+
expect(res.status).toHaveBeenCalledWith(200);
|
|
198
|
+
const body = res.json.mock.calls[0][0];
|
|
199
|
+
expect(body.user).toBeUndefined();
|
|
200
|
+
expect(body.access_token).toBe(body.token);
|
|
201
|
+
const jwt = require("jsonwebtoken");
|
|
202
|
+
const decoded = jwt.decode(body.token);
|
|
203
|
+
expect(decoded.userId).toBeUndefined();
|
|
204
|
+
expect(decoded.mobile).toBe("7903658469");
|
|
165
205
|
});
|
|
166
206
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "powr-sdk-api",
|
|
3
|
-
"version": "4.21.
|
|
3
|
+
"version": "4.21.1",
|
|
4
4
|
"description": "Shared API core library for PowrStack projects. Zero dependencies - works with Express, Next.js API routes, and other frameworks. All features are optional and install only what you need.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|