Split user
This commit is contained in:
@@ -4,61 +4,101 @@ const { body, validationResult } = require('express-validator')
|
||||
const { secretKey } = require('../authentication')
|
||||
const { _sr } = require('../plugins/serverResponses')
|
||||
const { checkValidations } = require('../plugins/controllersHelperFunctions')
|
||||
const User = require('../models/User')
|
||||
const UserGPS = require('../models/GPS.User')
|
||||
const { generateRandomDigits
|
||||
} = require('../plugins/controllersHelperFunctions')
|
||||
module.exports.login_with_pass = [
|
||||
[
|
||||
body('username')
|
||||
.isLength({ min: 4 })
|
||||
.withMessage(_sr.fa.min_char.min4)
|
||||
.bail()
|
||||
.if((value, { req }) => {
|
||||
return req.body.password && req.body.password.length >= 4
|
||||
})
|
||||
.custom((value, { req }) => {
|
||||
return UserGPS.findOne({
|
||||
$or: [{ national_code: value }, { mobile_number: value }]
|
||||
}).then(user => {
|
||||
if (!user) return Promise.reject(_sr.fa.not_found.password)
|
||||
else return true
|
||||
})
|
||||
}),
|
||||
body('password')
|
||||
.notEmpty()
|
||||
.withMessage(_sr.fa.required.password)
|
||||
.bail()
|
||||
.isLength({ min: 4 })
|
||||
.withMessage(_sr.fa.min_char.min4),
|
||||
body('remember_me')
|
||||
.exists()
|
||||
.withMessage(_sr.fa.required.remember_me)
|
||||
.bail()
|
||||
.isBoolean()
|
||||
.withMessage(_sr.fa.format.boolean)
|
||||
],
|
||||
checkValidations(validationResult),
|
||||
async (req, res) => {
|
||||
try {
|
||||
const { username, password } = req.body
|
||||
const user = await UserGPS.findOne({
|
||||
$or: [{ national_code: username }, { mobile_number: username }]
|
||||
})
|
||||
if (!user) throw new Error(_sr.fa.not_found.password)
|
||||
const passwordMatch = await bcrypt.compare(password, user.password)
|
||||
if (!passwordMatch) throw new Error(_sr.fa.not_found.password)
|
||||
if (!user.active) {
|
||||
throw new Error('حساب شما هنوز تاییده نشده است')
|
||||
}
|
||||
const token = await jwt.sign({ _id: user._id }, secretKey, {
|
||||
expiresIn: req.body.remember_me ? '30d' : '7d'
|
||||
})
|
||||
user.token = token
|
||||
await user.save()
|
||||
return res.status(200).json({ token })
|
||||
} catch (err) {
|
||||
return res.status(422).json({
|
||||
validation: { username: { msg: err.message } }
|
||||
})
|
||||
}
|
||||
}
|
||||
]
|
||||
|
||||
module.exports.login = [
|
||||
[
|
||||
body('username')
|
||||
.isLength({ min: 4 })
|
||||
.withMessage(_sr.fa.min_char.min4)
|
||||
.bail()
|
||||
.if((value, { req }) => {
|
||||
return req.body.password && req.body.password.length >= 4
|
||||
})
|
||||
.custom((value, { req }) => {
|
||||
return User.findOne({
|
||||
$or: [{ email: value.toLowerCase().trim() }, { national_code: value }, { username: value }]
|
||||
}).then(user => {
|
||||
if (!user) return Promise.reject(_sr.fa.not_found.password)
|
||||
else return true
|
||||
})
|
||||
}),
|
||||
body('password')
|
||||
.notEmpty()
|
||||
.withMessage(_sr.fa.required.password)
|
||||
.bail()
|
||||
.isLength({ min: 4 })
|
||||
.withMessage(_sr.fa.min_char.min4),
|
||||
body('remember_me')
|
||||
.exists()
|
||||
.withMessage(_sr.fa.required.remember_me)
|
||||
.bail()
|
||||
.isBoolean()
|
||||
.withMessage(_sr.fa.format.boolean)
|
||||
],
|
||||
checkValidations(validationResult),
|
||||
async (req, res) => {
|
||||
try {
|
||||
const { username, password } = req.body
|
||||
const user = await User.findOne({
|
||||
$or: [{ email: username.toLowerCase().trim() }, { national_code: username }, { username }]
|
||||
})
|
||||
if (!user) throw new Error(_sr.fa.not_found.password)
|
||||
const passwordMatch = await bcrypt.compare(password, user.password)
|
||||
if (!passwordMatch) throw new Error(_sr.fa.not_found.password)
|
||||
|
||||
module.exports.login_with_otp = [
|
||||
[
|
||||
body('mobile_number')
|
||||
.notEmpty()
|
||||
.withMessage(_sr.fa.required.phone_number),
|
||||
body('otp')
|
||||
.notEmpty()
|
||||
.withMessage(_sr.fa.required.phone_number)
|
||||
],
|
||||
checkValidations(validationResult),
|
||||
async (req, res) => {
|
||||
try {
|
||||
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) {
|
||||
if (!user.active) {
|
||||
throw new Error('حساب شما هنوز تاییده نشده است')
|
||||
}
|
||||
const token = await jwt.sign({ _id: user._id }, secretKey, {
|
||||
expiresIn: req.body.remember_me ? '30d' : '7d'
|
||||
})
|
||||
if(!user.scope.includes('gps')){
|
||||
throw new Error('شما به این بخش دسترسی ندارید')
|
||||
}
|
||||
user.token = token
|
||||
await user.save()
|
||||
user.otp = generateRandomDigits(6)
|
||||
user.save()
|
||||
return res.status(200).json({ token })
|
||||
} catch (err) {
|
||||
return res.status(422).json({
|
||||
validation: { username: { msg: err.message } }
|
||||
})
|
||||
|
||||
}
|
||||
} catch (err) {
|
||||
return res.status(422).json({
|
||||
validation: { otp: { msg: err.message } }
|
||||
})
|
||||
|
||||
}
|
||||
]
|
||||
|
||||
}
|
||||
]
|
||||
Reference in New Issue
Block a user