powr-sdk-api 4.21.1 → 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/routes/auth.js +39 -7
- package/dist/routes/auth.otp.test.js +44 -0
- package/package.json +1 -1
package/dist/routes/auth.js
CHANGED
|
@@ -314,6 +314,38 @@ router.post("/register", async (req, res) => {
|
|
|
314
314
|
});
|
|
315
315
|
}
|
|
316
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
|
+
});
|
|
317
349
|
|
|
318
350
|
// Login User
|
|
319
351
|
router.post("/login", async (req, res) => {
|
|
@@ -644,9 +676,9 @@ router.post("/reset-password", async (req, res) => {
|
|
|
644
676
|
// Admin views another account on this project. Access token only.
|
|
645
677
|
// Does not change the target password or refresh token.
|
|
646
678
|
router.post("/impersonate", verifyToken, async (req, res) => {
|
|
647
|
-
var _req$
|
|
679
|
+
var _req$body3;
|
|
648
680
|
const projectId = req.projectId;
|
|
649
|
-
const phoneOrEmail = String(((_req$
|
|
681
|
+
const phoneOrEmail = String(((_req$body3 = req.body) === null || _req$body3 === void 0 ? void 0 : _req$body3.phoneOrEmail) || "").trim();
|
|
650
682
|
try {
|
|
651
683
|
var _req$user;
|
|
652
684
|
if (!phoneOrEmail) {
|
|
@@ -750,8 +782,8 @@ router.post("/impersonate/exit", verifyToken, async (req, res) => {
|
|
|
750
782
|
}
|
|
751
783
|
});
|
|
752
784
|
router.post("/send-otp", async (req, res) => {
|
|
753
|
-
var _req$
|
|
754
|
-
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));
|
|
755
787
|
if (mobile.length !== 10) {
|
|
756
788
|
return res.status(400).json({
|
|
757
789
|
success: false,
|
|
@@ -804,9 +836,9 @@ router.post("/send-otp", async (req, res) => {
|
|
|
804
836
|
}
|
|
805
837
|
});
|
|
806
838
|
router.post("/verify-otp", async (req, res) => {
|
|
807
|
-
var _req$
|
|
808
|
-
const mobile = tenDigitMobile(((_req$
|
|
809
|
-
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, "");
|
|
810
842
|
if (mobile.length !== 10 || !otp) {
|
|
811
843
|
return res.status(400).json({
|
|
812
844
|
success: false,
|
|
@@ -203,4 +203,48 @@ describe("auth OTP routes", () => {
|
|
|
203
203
|
expect(decoded.userId).toBeUndefined();
|
|
204
204
|
expect(decoded.mobile).toBe("7903658469");
|
|
205
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
|
+
});
|
|
249
|
+
});
|
|
206
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",
|