corebasic 1.0.261 → 1.0.262

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/libs/auth.js CHANGED
@@ -42,7 +42,7 @@ const Apps = {
42
42
  Portal: "Portal"
43
43
  };
44
44
  async function attemptLogin(req) {
45
- let meta = { company: "GLOBAL", outlet: "GLOBAL" };
45
+ const globalMeta = Dip.globalMeta({ suffix: req.body.app });
46
46
  let errMessage = { success: false, message: "Login Server Error" };
47
47
  if (!req.body?.app || !Apps[req.body.app])
48
48
  throw { ...errMessage, mode: 'verify', info: "Invalid app" };
@@ -51,16 +51,15 @@ async function attemptLogin(req) {
51
51
  const code = req.body.code ?? '';
52
52
  let mob = userMob.endsWith('123456789') ? '0123456789' : Utils.parseMob(userMob, code);
53
53
  let time = new Date().getTime();
54
- const collection = `${req.body.app}.auth.login`;
55
54
  let otpValid = Utils.isEmpty(req.body.otp) ? false : true;
56
55
  let clientId = req.body.clientId;
57
56
  if (Utils.isEmpty(clientId))
58
57
  throw { ...errMessage, mode: otpValid ? 'verify' : 'login', info: "Invalid Client ID" };
59
58
  if (otpValid) { // verify login
60
- let res = await Dip.query(meta, collection, { _id: mob, otp: req.body.otp, clientId, time: { $gt: time - expiry } });
59
+ let res = await Dip.query(globalMeta, "auths", { _id: mob, otp: req.body.otp, clientId, time: { $gt: time - expiry } });
61
60
  if (res.length) {
62
61
  try {
63
- await Dip.update(meta, collection, { _id: mob }, { $set: { otp: '', clientId: '', loggedIn: true } });
62
+ await Dip.update(globalMeta, "auths", { _id: mob }, { $set: { otp: '', clientId: '', loggedIn: true } });
64
63
  }
65
64
  catch (err) {
66
65
  throw { ...errMessage, mode: 'verify', info: 'Cleanup Failed' };
@@ -76,7 +75,7 @@ async function attemptLogin(req) {
76
75
  let now = Utils.now().getTime();
77
76
  let data = req.body.data ?? {};
78
77
  let info = { clientId, created: now, updated: now, ...data };
79
- await Dip.update(meta, collection, { _id: mob }, { $set: { otp, clientId, time, updated: now, attemptLogin: true }, $setOnInsert: { _id: mob, otp, time, userId, ...info } }, { upsert: true });
78
+ await Dip.update(globalMeta, "auths", { _id: mob }, { $set: { otp, clientId, time, updated: now, attemptLogin: true }, $setOnInsert: { _id: mob, otp, time, userId, ...info } }, { upsert: true });
80
79
  return { mode: 'login', success: true, userId, expiry, phone: mob, mob, code };
81
80
  }
82
81
  throw { ...errMessage, mode: 'login', info: 'Generating Info Failed' };
package/libs/auth.ts CHANGED
@@ -97,32 +97,30 @@ type Auth = {
97
97
  loggedIn?: boolean
98
98
  }
99
99
  declare global {
100
- interface Schema extends Record<`${keyof typeof Apps}.auth.login`, Auth> {}
100
+ interface Schema extends Record<"auths", Auth> {}
101
101
  }
102
102
 
103
103
  async function attemptLogin(req: AuthRequest) {
104
- let meta = {company: "GLOBAL", outlet: "GLOBAL"}
104
+ const globalMeta = Dip.globalMeta({suffix: req.body.app})
105
105
  let errMessage = {success: false, message: "Login Server Error"}
106
106
 
107
107
  if (!req.body?.app || !Apps[req.body.app as keyof typeof Apps])
108
108
  throw {...errMessage, mode: 'verify', info: "Invalid app"}
109
109
 
110
-
111
110
  let expiry = 300000
112
111
  let userMob = req.body.mob ?? req.body.phone ?? ''
113
112
  const code = req.body.code ?? ''
114
113
  let mob = userMob.endsWith('123456789') ? '0123456789' : Utils.parseMob(userMob, code)
115
114
  let time = new Date().getTime()
116
- const collection = `${req.body.app}.auth.login` as `${keyof typeof Apps}.auth.login`;
117
115
  let otpValid = Utils.isEmpty(req.body.otp) ? false : true
118
116
  let clientId = req.body.clientId
119
117
  if (Utils.isEmpty(clientId))
120
118
  throw { ...errMessage, mode: otpValid ? 'verify' : 'login', info: "Invalid Client ID" }
121
119
 
122
120
  if (otpValid) { // verify login
123
- let res = await Dip.query(meta, collection, { _id: mob, otp: req.body.otp, clientId, time: { $gt: time - expiry } })
121
+ let res = await Dip.query(globalMeta, "auths", { _id: mob, otp: req.body.otp, clientId, time: { $gt: time - expiry } })
124
122
  if (res.length) {
125
- try {await Dip.update(meta, collection, { _id: mob }, { $set: { otp: '', clientId: '', loggedIn: true } }) } catch (err) { throw {...errMessage, mode: 'verify', info: 'Cleanup Failed'} }
123
+ try {await Dip.update(globalMeta, "auths", { _id: mob }, { $set: { otp: '', clientId: '', loggedIn: true } }) } catch (err) { throw {...errMessage, mode: 'verify', info: 'Cleanup Failed'} }
126
124
  return {...res[0], mode: 'verify', success: true, userId: res[0]!.userId!, phone: res[0]!._id, mob: res[0]!._id, code}
127
125
  }
128
126
  throw {...errMessage, mode: 'verify', info: "Invalid/Expired OTP"}
@@ -133,7 +131,7 @@ async function attemptLogin(req: AuthRequest) {
133
131
  let now = Utils.now().getTime()
134
132
  let data = req.body.data ?? {}
135
133
  let info = { clientId, created: now, updated: now, ...data}
136
- await Dip.update(meta, collection, { _id: mob }, { $set: { otp, clientId, time, updated: now, attemptLogin: true }, $setOnInsert: { _id: mob, otp, time, userId, ...info } }, { upsert: true })
134
+ await Dip.update(globalMeta, "auths", { _id: mob }, { $set: { otp, clientId, time, updated: now, attemptLogin: true }, $setOnInsert: { _id: mob, otp, time, userId, ...info } }, { upsert: true })
137
135
 
138
136
  return {mode: 'login', success: true, userId, expiry, phone: mob, mob, code}
139
137
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "corebasic",
3
3
  "type": "module",
4
- "version": "1.0.261",
4
+ "version": "1.0.262",
5
5
  "description": "",
6
6
  "main": "dist/index.js",
7
7
  "types": "./index.ts",