front-end almost done | started back-end
This commit is contained in:
@@ -0,0 +1,137 @@
|
||||
const Admin = require('../models/admin/Admin')
|
||||
const {body, validationResult} = require('express-validator')
|
||||
const dateformat = require('dateformat')
|
||||
const bcrypt = require('bcryptjs')
|
||||
const jwt = require('jsonwebtoken')
|
||||
const config = require('../config')
|
||||
|
||||
/////////////////////////////////////////////////////////////////////// register
|
||||
module.exports.register = [
|
||||
[
|
||||
body('name')
|
||||
.isLength({min: 4}).withMessage('حداقل 4 کاراکتر'),
|
||||
|
||||
body('username')
|
||||
.isLength({min: 4}).withMessage('حداقل 4 کاراکتر')
|
||||
.custom((value, {req}) => {
|
||||
return Admin.findOne({username: value})
|
||||
.then(admin => {
|
||||
if (admin && admin.username === value) return Promise.reject('این نام کاربری از قبل وجود دارد.')
|
||||
else return true
|
||||
})
|
||||
}),
|
||||
|
||||
body('password')
|
||||
.isLength({min: 4}).withMessage('حداقل 4 کاراکتر')
|
||||
.custom((value, {req}) => {
|
||||
return value === req.body.password_confirmation
|
||||
}).withMessage('پسورد ها یکی نیست.')
|
||||
],
|
||||
(req, res) => {
|
||||
const errors = validationResult(req)
|
||||
if (!errors.isEmpty()) return res.status(422).json({validation: errors.mapped()})
|
||||
|
||||
const data = {}
|
||||
data.name = req.body.name
|
||||
data.username = req.body.username
|
||||
data.scope = 'admin'
|
||||
data.created_at = dateformat(Date.now(), 'yyyy-mm-dd HH:MM:ss')
|
||||
|
||||
bcrypt.genSalt(10, (err, salt) => {
|
||||
bcrypt.hash(req.body.password, salt, (err, hash) => {
|
||||
if (err) console.log(err)
|
||||
data.password = hash
|
||||
|
||||
const admin = new Admin(data)
|
||||
admin.save(err => {
|
||||
if (err) console.log(err)
|
||||
return res.json({message: 'Admin added.'})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
}
|
||||
]
|
||||
|
||||
/////////////////////////////////////////////////////////////////////// login
|
||||
module.exports.login = [
|
||||
[
|
||||
body('username')
|
||||
.isLength({min: 4}).withMessage('حداقل 4 کاراکتر')
|
||||
.custom((value, {req}) => {
|
||||
return Admin.findOne({username: value})
|
||||
.then(admin => {
|
||||
if (!admin) return Promise.reject('ادمینی با این نام وجود ندارد.')
|
||||
else return true
|
||||
})
|
||||
}),
|
||||
|
||||
body('password')
|
||||
.isLength({min: 4}).withMessage('حداقل 4 کاراکتر'),
|
||||
|
||||
body('remember_me')
|
||||
.exists().withMessage('پارامتر remember_me الزامی است.')
|
||||
.bail()
|
||||
.isBoolean().withMessage('مقدار باید از جنس Boolean باشد.')
|
||||
],
|
||||
(req, res) => {
|
||||
const errors = validationResult(req)
|
||||
if (!errors.isEmpty()) return res.status(422).json({validation: errors.mapped()})
|
||||
|
||||
Admin.findOne({username: req.body.username}, (err, user) => {
|
||||
if (err) console.log(err)
|
||||
bcrypt.compare(req.body.password, user.password, (err, isMatch) => {
|
||||
if (err) console.log(err)
|
||||
if (!isMatch) return res.status(422).json({validation: {password: {msg: 'پسورد اشتباه است.'}}})
|
||||
|
||||
let token
|
||||
if (req.body.remember_me) token = jwt.sign({_id: user._id}, config.secretKey)
|
||||
else token = jwt.sign({_id: user._id}, config.secretKey, {expiresIn: '1h'})
|
||||
|
||||
user.token = token
|
||||
user.markModified('token')
|
||||
user.save(err => {
|
||||
if (err) console.log(err)
|
||||
})
|
||||
return res.json({token: token})
|
||||
})
|
||||
})
|
||||
}
|
||||
]
|
||||
|
||||
/////////////////////////////////////////////////////////////////////// logout
|
||||
module.exports.logout = [
|
||||
(req, res) => {
|
||||
const token = req.headers.authorization
|
||||
if (token) {
|
||||
Admin.findOneAndUpdate({token: token}, {token: null}, (err, oldData) => {
|
||||
if (err) console.log(err)
|
||||
if (oldData) return res.json({message: 'You are logged out.'})
|
||||
return res.status(401).json({message: 'Your not logged in'})
|
||||
})
|
||||
} else {
|
||||
return res.status(401).json({message: 'Your not logged in'})
|
||||
}
|
||||
}
|
||||
]
|
||||
|
||||
/////////////////////////////////////////////////////////////////////// get user
|
||||
module.exports.getUser = [
|
||||
config.isAdmin,
|
||||
(req, res) => {
|
||||
const token = req.headers.authorization
|
||||
if (token) {
|
||||
jwt.verify(token, config.secretKey, (err, decoded) => {
|
||||
if (err) return res.status(401).json({message: 'unauthenticated'})
|
||||
Admin.findById(decoded._id, (err, data) => {
|
||||
if (err) console.log(err)
|
||||
return res.json({user: data})
|
||||
})
|
||||
})
|
||||
} else {
|
||||
return res.status(401).json({message: 'unauthenticated'})
|
||||
}
|
||||
}
|
||||
]
|
||||
|
||||
/////////////////////////////////////////////////////////////////////// delete
|
||||
Reference in New Issue
Block a user