corebasic 1.0.26 → 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 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(406).json({ mode: 'login', success: false, msg: validateErrMessage ?? 'Validation Failed' })
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 {
@@ -28,7 +28,7 @@ module.exports.start = (app, successCallback) => {
28
28
  } else
29
29
  res.json(login)
30
30
  } catch (err) {
31
- return res.status(406).json(err)
31
+ return res.status(401).json(err)
32
32
  }
33
33
  })
34
34
  }
@@ -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
- if (req.body.otp) { // verify login
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
- throw {mode: 'verify', success: false, msg: "Invalid OTP"}
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', success: false, msg: "Login Server Error"}
60
+ throw {...errMessage, mode: 'login', info: 'Generating Info Failed'}
57
61
  }
58
62
  }
59
63
 
package/libs/session.js CHANGED
@@ -31,7 +31,7 @@ function start(expressApp, allowedUrls) {
31
31
  return next()
32
32
  throw null;
33
33
  } catch (error) {
34
- return res.status(406).json({ message: 'Unauthorized' })
34
+ return res.status(401).json({ message: 'Unauthorized' })
35
35
  }
36
36
  })
37
37
 
@@ -49,7 +49,7 @@ function start(expressApp, allowedUrls) {
49
49
 
50
50
  } catch (error) {
51
51
  // Access Denied
52
- return res.status(406).json({ message: 'Unauthorized' })
52
+ return res.status(400).json({ message: 'Unauthorized' })
53
53
  }
54
54
  });
55
55
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "corebasic",
3
- "version": "1.0.26",
3
+ "version": "1.0.28",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {