corebasic 1.0.12 → 1.0.14

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 +7 -5
  2. package/package.json +4 -2
package/libs/auth.js CHANGED
@@ -6,11 +6,11 @@ const Utils = require('./utils')
6
6
  const Session = require('./session')
7
7
 
8
8
  let validateFn, validateErrMessage
9
- export function validate(callback, errMessage){
9
+ module.exports.validate = (callback, errMessage){
10
10
  validateFn = callback
11
11
  }
12
12
 
13
- export function start(app, successCallback) {
13
+ module.exports.start = (app, successCallback) {
14
14
  app.post("/login", async (req, res) => {
15
15
  if (validateFn && !validateFn(req)) {
16
16
  res.json({mode: 'login', success: false, msg: validateErrMessage ?? 'Validation Failed'})
@@ -51,7 +51,9 @@ async function login(req, res) {
51
51
  let otp = otpGenerator.generate(4, { upperCaseAlphabets: false, specialChars: false, digital: true, lowerCaseAlphabets: false });
52
52
  if (await sendOtp(phone, otp)) {
53
53
  let userId = req.body.userId ?? Utils.uid()
54
- await Elabase.update(collection, { _id: phone.trim() }, { $set: { otp, time }, $setOnInsert: { _id: phone.trim(), otp, time, userId } }, { upsert: true })
54
+ let now = Utils.now().toISOString()
55
+ let meta = {createdAt: now, updatedAt: now, pos: req.body.pos ?? undefined}
56
+ await Elabase.update(collection, { _id: phone.trim() }, { $set: { otp, time, updatedAt: now }, $setOnInsert: { _id: phone.trim(), otp, time, userId, ...meta } }, { upsert: true })
55
57
  return {mode: 'login', success: true, userId}
56
58
  }
57
59
  throw false
@@ -76,8 +78,8 @@ async function sendOtp(phone, otp) {
76
78
 
77
79
  // API
78
80
  // ----
79
- // Login => HttpRequest.post('/login').send({app: '', clientId: '', phone: '', otp: '', userId: 'on signup'}) // Response => { mode: 'login', success, userId }
80
- // Verify => HttpRequest.post('/login').send({app: '', clientId: '', phone: '', otp: ''}) // Response => { mode: 'verify', success, userId }
81
+ // Login => HttpRequest.post('/login').send({app: '', clientId: '', phone: '', userId: 'Optional'}) // Response => { mode: 'login', success, userId }
82
+ // Verify => HttpRequest.post('/login').send({app: '', clientId: '', phone: '', otp: ''}) // Response => { mode: 'verify', success, userId }
81
83
 
82
84
  // Usage
83
85
  // Auth.validate((req) => ["Phone1", "Phone2"].includes(req.body.phone.trim()))
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "corebasic",
3
- "version": "1.0.12",
3
+ "version": "1.0.14",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -9,6 +9,8 @@
9
9
  "author": "",
10
10
  "license": "ISC",
11
11
  "dependencies": {
12
- "jsonwebtoken": "^9.0.1"
12
+ "axios": "^1.4.0",
13
+ "jsonwebtoken": "^9.0.1",
14
+ "otp-generator": "^4.0.1"
13
15
  }
14
16
  }