corebasic 1.0.27 → 1.0.28
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/libs/auth.js +9 -5
- package/package.json +1 -1
package/libs/auth.js
CHANGED
|
@@ -13,7 +13,7 @@ module.exports.validate = (callback, errMessage) => {
|
|
|
13
13
|
module.exports.start = (app, successCallback) => {
|
|
14
14
|
app.post("/login", async (req, res) => {
|
|
15
15
|
if (validateFn && !validateFn(req)) {
|
|
16
|
-
res.status(401).json({ mode: 'login', success: false,
|
|
16
|
+
res.status(401).json({ mode: 'login', success: false, info: validateErrMessage ?? 'Validation Failed', message: 'Login Server Error' })
|
|
17
17
|
return
|
|
18
18
|
}
|
|
19
19
|
try {
|
|
@@ -38,11 +38,15 @@ async function attemptLogin(req, res) {
|
|
|
38
38
|
let phone = req.body.phone.trim()
|
|
39
39
|
let time = new Date().getTime()
|
|
40
40
|
let collection = (req.body.app ? req.body.app + '.' : '') + "auth.login"
|
|
41
|
-
|
|
41
|
+
let errMessage = {success: false, message: "Login Server Error"}
|
|
42
|
+
let otpValid = Utils.isEmpty(req.body.otp) ? false : true
|
|
43
|
+
if (otpValid) { // verify login
|
|
42
44
|
let res = await Elabase.query(collection, { _id: phone, otp: req.body.otp, time: { $gt: time - expiry } })
|
|
43
|
-
if (res.length)
|
|
45
|
+
if (res.length) {
|
|
46
|
+
try {await Elabase.update(collection, { _id: phone }, { $set: { otp: '' } }) } catch (err) { throw {...errMessage, mode: 'verify', info: 'Cleanup Failed'} }
|
|
44
47
|
return {...res[0], mode: 'verify', success: true, userId: res[0].userId, position: res[0].position, phone: res[0]._id}
|
|
45
|
-
|
|
48
|
+
}
|
|
49
|
+
throw {...errMessage, mode: 'verify', info: "Invalid/Expired OTP"}
|
|
46
50
|
} else { // generate login
|
|
47
51
|
let otp = phone === '0123456789' ? '1234' : otpGenerator.generate(4, { upperCaseAlphabets: false, specialChars: false, digital: true, lowerCaseAlphabets: false });
|
|
48
52
|
if (phone === '0123456789' || await sendOtp(phone, otp)) {
|
|
@@ -53,7 +57,7 @@ async function attemptLogin(req, res) {
|
|
|
53
57
|
await Elabase.update(collection, { _id: phone }, { $set: { otp, time, updatedAt: now }, $setOnInsert: { _id: phone, otp, time, userId, ...meta } }, { upsert: true })
|
|
54
58
|
return {mode: 'login', success: true, userId, expiry, phone}
|
|
55
59
|
}
|
|
56
|
-
throw {mode: 'login',
|
|
60
|
+
throw {...errMessage, mode: 'login', info: 'Generating Info Failed'}
|
|
57
61
|
}
|
|
58
62
|
}
|
|
59
63
|
|