powr-sdk-api 4.21.0 → 4.22.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/dist/middleware/jwtToken.js +21 -0
- package/dist/routes/auth.js +48 -11
- package/dist/routes/auth.otp.test.js +86 -2
- package/package.json +1 -1
|
@@ -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
|
|
@@ -313,6 +314,38 @@ router.post("/register", async (req, res) => {
|
|
|
313
314
|
});
|
|
314
315
|
}
|
|
315
316
|
});
|
|
317
|
+
router.post("/login-options", async (req, res) => {
|
|
318
|
+
var _req$body, _req$body2;
|
|
319
|
+
const phoneOrEmail = String(((_req$body = req.body) === null || _req$body === void 0 ? void 0 : _req$body.phoneOrEmail) || ((_req$body2 = req.body) === null || _req$body2 === void 0 ? void 0 : _req$body2.mobile) || "").trim();
|
|
320
|
+
try {
|
|
321
|
+
const q = buildIdentifierQuery({
|
|
322
|
+
phoneOrEmail
|
|
323
|
+
});
|
|
324
|
+
if (!q) {
|
|
325
|
+
return res.status(400).json({
|
|
326
|
+
success: false,
|
|
327
|
+
message: "Valid phone is required"
|
|
328
|
+
});
|
|
329
|
+
}
|
|
330
|
+
const db = await getDb();
|
|
331
|
+
const user = await db.collection("users").findOne(q, {
|
|
332
|
+
projection: {
|
|
333
|
+
password: 1
|
|
334
|
+
}
|
|
335
|
+
});
|
|
336
|
+
return res.status(200).json({
|
|
337
|
+
success: true,
|
|
338
|
+
exists: Boolean(user),
|
|
339
|
+
hasPassword: Boolean(user && user.password)
|
|
340
|
+
});
|
|
341
|
+
} catch (error) {
|
|
342
|
+
console.error("Error in login-options:", error);
|
|
343
|
+
return res.status(500).json({
|
|
344
|
+
success: false,
|
|
345
|
+
message: "Could not check login."
|
|
346
|
+
});
|
|
347
|
+
}
|
|
348
|
+
});
|
|
316
349
|
|
|
317
350
|
// Login User
|
|
318
351
|
router.post("/login", async (req, res) => {
|
|
@@ -643,9 +676,9 @@ router.post("/reset-password", async (req, res) => {
|
|
|
643
676
|
// Admin views another account on this project. Access token only.
|
|
644
677
|
// Does not change the target password or refresh token.
|
|
645
678
|
router.post("/impersonate", verifyToken, async (req, res) => {
|
|
646
|
-
var _req$
|
|
679
|
+
var _req$body3;
|
|
647
680
|
const projectId = req.projectId;
|
|
648
|
-
const phoneOrEmail = String(((_req$
|
|
681
|
+
const phoneOrEmail = String(((_req$body3 = req.body) === null || _req$body3 === void 0 ? void 0 : _req$body3.phoneOrEmail) || "").trim();
|
|
649
682
|
try {
|
|
650
683
|
var _req$user;
|
|
651
684
|
if (!phoneOrEmail) {
|
|
@@ -749,8 +782,8 @@ router.post("/impersonate/exit", verifyToken, async (req, res) => {
|
|
|
749
782
|
}
|
|
750
783
|
});
|
|
751
784
|
router.post("/send-otp", async (req, res) => {
|
|
752
|
-
var _req$
|
|
753
|
-
const mobile = tenDigitMobile(((_req$
|
|
785
|
+
var _req$body4, _req$body5;
|
|
786
|
+
const mobile = tenDigitMobile(((_req$body4 = req.body) === null || _req$body4 === void 0 ? void 0 : _req$body4.mobile) || ((_req$body5 = req.body) === null || _req$body5 === void 0 ? void 0 : _req$body5.phoneOrEmail));
|
|
754
787
|
if (mobile.length !== 10) {
|
|
755
788
|
return res.status(400).json({
|
|
756
789
|
success: false,
|
|
@@ -803,9 +836,9 @@ router.post("/send-otp", async (req, res) => {
|
|
|
803
836
|
}
|
|
804
837
|
});
|
|
805
838
|
router.post("/verify-otp", async (req, res) => {
|
|
806
|
-
var _req$
|
|
807
|
-
const mobile = tenDigitMobile(((_req$
|
|
808
|
-
const otp = String(((_req$
|
|
839
|
+
var _req$body6, _req$body7, _req$body8;
|
|
840
|
+
const mobile = tenDigitMobile(((_req$body6 = req.body) === null || _req$body6 === void 0 ? void 0 : _req$body6.mobile) || ((_req$body7 = req.body) === null || _req$body7 === void 0 ? void 0 : _req$body7.phoneOrEmail));
|
|
841
|
+
const otp = String(((_req$body8 = req.body) === null || _req$body8 === void 0 ? void 0 : _req$body8.otp) || "").replace(/\D/g, "");
|
|
809
842
|
if (mobile.length !== 10 || !otp) {
|
|
810
843
|
return res.status(400).json({
|
|
811
844
|
success: false,
|
|
@@ -876,9 +909,12 @@ router.post("/verify-otp", async (req, res) => {
|
|
|
876
909
|
});
|
|
877
910
|
const user = q ? await db.collection("users").findOne(q) : null;
|
|
878
911
|
if (!user) {
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
912
|
+
const token = generatePhoneProofToken(mobile);
|
|
913
|
+
return res.status(200).json({
|
|
914
|
+
success: true,
|
|
915
|
+
message: "OTP verified successfully.",
|
|
916
|
+
token,
|
|
917
|
+
access_token: token
|
|
882
918
|
});
|
|
883
919
|
}
|
|
884
920
|
const {
|
|
@@ -891,7 +927,8 @@ router.post("/verify-otp", async (req, res) => {
|
|
|
891
927
|
message: "OTP verified successfully.",
|
|
892
928
|
user: publicUser(user, profile),
|
|
893
929
|
token,
|
|
894
|
-
refreshToken
|
|
930
|
+
refreshToken,
|
|
931
|
+
access_token: token
|
|
895
932
|
});
|
|
896
933
|
} catch (error) {
|
|
897
934
|
console.error("Error in verify-otp:", error);
|
|
@@ -155,12 +155,96 @@ 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");
|
|
205
|
+
});
|
|
206
|
+
});
|
|
207
|
+
describe("auth login-options", () => {
|
|
208
|
+
it("reports exists and hasPassword", async () => {
|
|
209
|
+
const findOne = jest.fn().mockResolvedValue({
|
|
210
|
+
password: "hash"
|
|
211
|
+
});
|
|
212
|
+
getDb.mockResolvedValue({
|
|
213
|
+
collection: () => ({
|
|
214
|
+
findOne
|
|
215
|
+
})
|
|
216
|
+
});
|
|
217
|
+
const req = {
|
|
218
|
+
body: {
|
|
219
|
+
mobile: "7903658469"
|
|
220
|
+
}
|
|
221
|
+
};
|
|
222
|
+
const res = mockRes();
|
|
223
|
+
await findRoute("post", "/login-options")(req, res);
|
|
224
|
+
expect(res.status).toHaveBeenCalledWith(200);
|
|
225
|
+
expect(res.json.mock.calls[0][0]).toEqual({
|
|
226
|
+
success: true,
|
|
227
|
+
exists: true,
|
|
228
|
+
hasPassword: true
|
|
229
|
+
});
|
|
230
|
+
});
|
|
231
|
+
it("reports no user", async () => {
|
|
232
|
+
getDb.mockResolvedValue({
|
|
233
|
+
collection: () => ({
|
|
234
|
+
findOne: jest.fn().mockResolvedValue(null)
|
|
235
|
+
})
|
|
236
|
+
});
|
|
237
|
+
const req = {
|
|
238
|
+
body: {
|
|
239
|
+
phoneOrEmail: "+917903658469"
|
|
240
|
+
}
|
|
241
|
+
};
|
|
242
|
+
const res = mockRes();
|
|
243
|
+
await findRoute("post", "/login-options")(req, res);
|
|
244
|
+
expect(res.json.mock.calls[0][0]).toEqual({
|
|
245
|
+
success: true,
|
|
246
|
+
exists: false,
|
|
247
|
+
hasPassword: false
|
|
248
|
+
});
|
|
165
249
|
});
|
|
166
250
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "powr-sdk-api",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.22.0",
|
|
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",
|