feat: sms module
This commit is contained in:
@@ -5,9 +5,8 @@ const { secretKey } = require('../authentication')
|
||||
const { _sr } = require('../plugins/serverResponses')
|
||||
const { checkValidations } = require('../plugins/controllersHelperFunctions')
|
||||
const UserGPS = require('../models/GPS.User')
|
||||
const { generateRandomDigits
|
||||
} = require('../plugins/controllersHelperFunctions')
|
||||
const { SMS } = require('../plugins/SMS_Module')
|
||||
const { generateRandomDigits } = require('../plugins/controllersHelperFunctions')
|
||||
const sms = require('../plugins/SMS_Module')
|
||||
module.exports.login_with_pass = [
|
||||
[
|
||||
body('username')
|
||||
@@ -56,7 +55,7 @@ module.exports.login_with_pass = [
|
||||
})
|
||||
// eslint-disable-next-line eqeqeq
|
||||
if (user?.lastIP && user?.lastIP != req.ip) {
|
||||
SMS([user.mobile_number], "کاربر گرامی، \n با دستگاه جدیدی به اکانت شما در آسان سرویس وارد شدند")
|
||||
await sms.auth.deviceLogin(user.mobile_number, req.ip)
|
||||
}
|
||||
user.lastIP = req.ip
|
||||
user.token = token
|
||||
@@ -72,12 +71,8 @@ module.exports.login_with_pass = [
|
||||
|
||||
module.exports.login_with_otp = [
|
||||
[
|
||||
body('mobile_number')
|
||||
.notEmpty()
|
||||
.withMessage(_sr.fa.required.phone_number),
|
||||
body('otp')
|
||||
.notEmpty()
|
||||
.withMessage(_sr.fa.required.phone_number)
|
||||
body('mobile_number').notEmpty().withMessage(_sr.fa.required.phone_number),
|
||||
body('otp').notEmpty().withMessage(_sr.fa.required.phone_number)
|
||||
],
|
||||
checkValidations(validationResult),
|
||||
async (req, res) => {
|
||||
@@ -85,7 +80,7 @@ module.exports.login_with_otp = [
|
||||
const { mobile_number, otp } = req.body
|
||||
const user = await UserGPS.findOne({ mobile_number })
|
||||
if (!user) throw new Error(_sr.fa.not_found.user_id)
|
||||
if (user.otp !== otp) throw new Error("کد اشتباه است")
|
||||
if (user.otp !== otp) throw new Error('کد اشتباه است')
|
||||
if (user.otp === otp) {
|
||||
if (!user.active) {
|
||||
throw new Error('حساب شما هنوز تاییده نشده است')
|
||||
@@ -95,21 +90,18 @@ module.exports.login_with_otp = [
|
||||
})
|
||||
// eslint-disable-next-line eqeqeq
|
||||
if (user?.lastIP && user?.lastIP != req.ip) {
|
||||
SMS([user.mobile_number], "کاربر گرامی، \n با دستگاه جدیدی به اکانت شما در آسان سرویس وارد شدند")
|
||||
await sms.auth.deviceLogin(user.mobile_number, req.ip)
|
||||
}
|
||||
user.otp = generateRandomDigits(6)
|
||||
user.lastIP = req.ip
|
||||
user.token = token
|
||||
user.save()
|
||||
return res.status(200).json({ token })
|
||||
|
||||
}
|
||||
} catch (err) {
|
||||
return res.status(422).json({
|
||||
validation: { otp: { msg: err.message } }
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
]
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user