corebasic 1.0.27 → 1.0.29

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.
Files changed (2) hide show
  1. package/libs/auth.js +17 -9
  2. 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, 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 {
@@ -38,22 +38,30 @@ 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
42
- let res = await Elabase.query(collection, { _id: phone, otp: req.body.otp, time: { $gt: time - expiry } })
43
- if (res.length)
44
- 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"}
41
+ let errMessage = {success: false, message: "Login Server Error"}
42
+ let otpValid = Utils.isEmpty(req.body.otp) ? false : true
43
+ let clientId = req.body.clientId
44
+ if (Utils.isEmpty(clientId))
45
+ throw { ...errMessage, mode: otpValid ? 'verify' : 'login', info: "Invalid Client ID" }
46
+
47
+ if (otpValid) { // verify login
48
+ let res = await Elabase.query(collection, { _id: phone, otp: req.body.otp, clientId, time: { $gt: time - expiry } })
49
+ if (res.length) {
50
+ try {await Elabase.update(collection, { _id: phone }, { $set: { otp: '', clientId: '' } }) } catch (err) { throw {...errMessage, mode: 'verify', info: 'Cleanup Failed'} }
51
+ return {...res[0], mode: 'verify', success: true, userId: res[0].userId, phone: res[0]._id}
52
+ }
53
+ throw {...errMessage, mode: 'verify', info: "Invalid/Expired OTP"}
46
54
  } else { // generate login
47
55
  let otp = phone === '0123456789' ? '1234' : otpGenerator.generate(4, { upperCaseAlphabets: false, specialChars: false, digital: true, lowerCaseAlphabets: false });
48
56
  if (phone === '0123456789' || await sendOtp(phone, otp)) {
49
57
  let userId = req.body.userId ?? Utils.uid()
50
58
  let now = Utils.now().toISOString()
51
59
  let data = req.body.data ?? {}
52
- let meta = {createdAt: now, updatedAt: now, pos: req.body.pos ?? undefined, ...data}
53
- await Elabase.update(collection, { _id: phone }, { $set: { otp, time, updatedAt: now }, $setOnInsert: { _id: phone, otp, time, userId, ...meta } }, { upsert: true })
60
+ let meta = { clientId, createdAt: now, updatedAt: now, ...data}
61
+ await Elabase.update(collection, { _id: phone }, { $set: { otp, clientId, time, updatedAt: now }, $setOnInsert: { _id: phone, otp, time, userId, ...meta } }, { upsert: true })
54
62
  return {mode: 'login', success: true, userId, expiry, phone}
55
63
  }
56
- throw {mode: 'login', success: false, msg: "Login Server Error"}
64
+ throw {...errMessage, mode: 'login', info: 'Generating Info Failed'}
57
65
  }
58
66
  }
59
67
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "corebasic",
3
- "version": "1.0.27",
3
+ "version": "1.0.29",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {