|
1 | | -const Admin = require('../models/Admin'); |
2 | | -const joi = require('joi'); |
3 | | -const bcrypt = require('bcrypt'); |
4 | | - |
5 | | -const registerValidator = (data) => { |
6 | | - const schema = { |
7 | | - name: joi.string().max(55).required(), |
8 | | - userName: joi.string().max(100).required(), |
9 | | - password: joi.string().required(), |
10 | | - confirmPassword: joi.string().required() |
11 | | - } |
12 | | - |
13 | | - return joi.validate(data,schema); |
14 | | -} |
15 | | - |
16 | | -const updateValidator = data => { |
17 | | - const schema = { |
18 | | - name: joi.string().max(55).required(), |
19 | | - userName: joi.string().max(100).required(), |
20 | | - oldPassword: joi.string().required(), |
21 | | - newPassword: joi.string().required() |
22 | | - } |
23 | | - return joi.validate(data,schema); |
24 | | -} |
25 | | - |
26 | | -const loginValidator = data => { |
27 | | - const schema = { |
28 | | - userName: joi.string().max(256).required(), |
29 | | - password: joi.string().required() |
30 | | - } |
31 | | - return joi.validate(data,schema) |
32 | | -} |
33 | | - |
34 | | -const login = async (req,res) => { |
35 | | - const {error} = loginValidator(req.body); |
36 | | - if(error) return res.status(400).send(error.message); |
37 | | - const {userName,password} = req.body; |
38 | | - const admin = await Admin.findOne({userName}); |
39 | | - if(!admin) return res.status(400).send("usernaem or password is invalid1"); |
40 | | - const isValidPass = await bcrypt.compare(password,admin.password); |
41 | | - if(!isValidPass) return res.status(400).send("username or password is invalid2"); |
42 | | - const token = admin.getAdminToken(); |
43 | | - return res.header('admin_token',token).status(200).send(token); |
44 | | -} |
45 | | - |
46 | | - |
47 | | -const register = async (req,res) => { |
48 | | - const {error} = registerValidator(req.body); |
49 | | - if(error) return res.status(400).send(error.message); |
50 | | - |
51 | | - const {name,userName,password,confirmPassword} = req.body; |
52 | | - if(password !== confirmPassword) return res.status(400).send("confirm password doesn't match with password !") |
53 | | - const admin = await new Admin({name,userName,password}); |
54 | | - const salt = await bcrypt.genSalt(10); |
55 | | - admin.password = await bcrypt.hash(admin.password,salt); |
56 | | - await admin.save(); |
57 | | - if(!admin) return res.status(500).send("something wrong"); |
58 | | - return res.status(201).send(true); |
59 | | -} |
60 | | - |
61 | | -const getUpdateAdmin = async (req,res) => { |
62 | | - const {error} = updateValidator(req.body); |
63 | | - if(error) return res.status(400).send(error.message); |
64 | | - |
65 | | - const {name,userName,oldPassword,newPassword} = req.body; |
66 | | - const admin = await Admin.findById(req.admin._id); |
67 | | - const oldPassIsValid = await bcrypt.compare(oldPassword,admin.password); |
68 | | - if(!oldPassIsValid) return res.status(400).send("old password doesn't match!"); |
69 | | - const salt = await bcrypt.genSalt(10); |
70 | | - const password = await bcrypt.hash(newPassword,salt); |
71 | | - const updated = await Admin.findByIdAndUpdate({_id: req.admin._id},{$set: {name,userName,password}},{new: true}); |
72 | | - if(!updated) return res.status(500).send("something wrong"); |
73 | | - return res.status(200).send(updated); |
74 | | -} |
75 | | - |
76 | | -const getAllAdmin = async (req,res) => { |
77 | | - const admins = await Admin.find(); |
78 | | - if(!admins) return res.status(500).send("something wrong"); |
79 | | - return res.status(200).send(admins); |
80 | | -} |
81 | | - |
82 | | -const getSingleAdmin = async (req,res) => { |
83 | | - const admin = await Admin.findById(req.params.adminId); |
84 | | - if(!admin) return res.status(500).send("something wrong"); |
85 | | - return res.status(200).send(admin); |
86 | | -} |
87 | | - |
88 | | -const getMe = async (req,res) => { |
89 | | - const admin = await Admin.findById(req.admin._id); |
90 | | - if(!admin) return res.status(500).send("something wrong"); |
91 | | - return res.status(200).send(admin); |
92 | | -} |
93 | | - |
94 | | -const deleteAdmin = async (req,res) => { |
95 | | - const admin = await Admin.findByIdAndDelete(req.params.adminId); |
96 | | - if(!admin) return res.status(500).send("something wrong"); |
97 | | - return res.status(200).send(admin); |
98 | | -} |
99 | | - |
100 | | -module.exports = { |
101 | | - register, |
102 | | - getAllAdmin, |
103 | | - getSingleAdmin, |
104 | | - getMe, |
105 | | - deleteAdmin, |
106 | | - login, |
107 | | - getUpdateAdmin |
108 | | -} |
| 1 | +const Admin = require('../models/Admin'); |
| 2 | +const joi = require('joi'); |
| 3 | +const bcrypt = require('bcrypt'); |
| 4 | + |
| 5 | +const registerValidator = (data) => { |
| 6 | + const schema = { |
| 7 | + name: joi.string().max(55).required(), |
| 8 | + userName: joi.string().max(100).required(), |
| 9 | + password: joi.string().required(), |
| 10 | + confirmPassword: joi.string().required() |
| 11 | + } |
| 12 | + |
| 13 | + return joi.validate(data,schema); |
| 14 | +} |
| 15 | + |
| 16 | +const updateValidator = data => { |
| 17 | + const schema = { |
| 18 | + name: joi.string().max(55).required(), |
| 19 | + userName: joi.string().max(100).required(), |
| 20 | + oldPassword: joi.string().required(), |
| 21 | + newPassword: joi.string().required() |
| 22 | + } |
| 23 | + return joi.validate(data,schema); |
| 24 | +} |
| 25 | + |
| 26 | +const loginValidator = data => { |
| 27 | + const schema = { |
| 28 | + userName: joi.string().max(256).required(), |
| 29 | + password: joi.string().required() |
| 30 | + } |
| 31 | + return joi.validate(data,schema) |
| 32 | +} |
| 33 | + |
| 34 | +const login = async (req,res) => { |
| 35 | + const {error} = loginValidator(req.body); |
| 36 | + if(error) return res.status(400).send(error.message); |
| 37 | + const {userName,password} = req.body; |
| 38 | + const admin = await Admin.findOne({userName}); |
| 39 | + if(!admin) return res.status(400).send("usernaem or password is invalid1"); |
| 40 | + const isValidPass = await bcrypt.compare(password,admin.password); |
| 41 | + if(!isValidPass) return res.status(400).send("username or password is invalid2"); |
| 42 | + const token = admin.getAdminToken(); |
| 43 | + return res.header('admin_token',token).status(200).send(token); |
| 44 | +} |
| 45 | + |
| 46 | + |
| 47 | +const register = async (req,res) => { |
| 48 | + const {error} = registerValidator(req.body); |
| 49 | + if(error) return res.status(400).send(error.message); |
| 50 | + |
| 51 | + const {name,userName,password,confirmPassword} = req.body; |
| 52 | + if(password !== confirmPassword) return res.status(400).send("confirm password doesn't match with password !") |
| 53 | + const admin = await new Admin({name,userName,password}); |
| 54 | + const salt = await bcrypt.genSalt(10); |
| 55 | + admin.password = await bcrypt.hash(admin.password,salt); |
| 56 | + await admin.save(); |
| 57 | + if(!admin) return res.status(500).send("something wrong"); |
| 58 | + return res.status(201).send(true); |
| 59 | +} |
| 60 | + |
| 61 | +const getUpdateAdmin = async (req,res) => { |
| 62 | + const {error} = updateValidator(req.body); |
| 63 | + if(error) return res.status(400).send(error.message); |
| 64 | + |
| 65 | + const {name,userName,oldPassword,newPassword} = req.body; |
| 66 | + const admin = await Admin.findById(req.admin._id); |
| 67 | + const oldPassIsValid = await bcrypt.compare(oldPassword,admin.password); |
| 68 | + if(!oldPassIsValid) return res.status(400).send("old password doesn't match!"); |
| 69 | + const salt = await bcrypt.genSalt(10); |
| 70 | + const password = await bcrypt.hash(newPassword,salt); |
| 71 | + const updated = await Admin.findByIdAndUpdate({_id: req.admin._id},{$set: {name,userName,password}},{new: true}); |
| 72 | + if(!updated) return res.status(500).send("something wrong"); |
| 73 | + return res.status(200).send(updated); |
| 74 | +} |
| 75 | + |
| 76 | +const getAllAdmin = async (req,res) => { |
| 77 | + const admins = await Admin.find(); |
| 78 | + if(!admins) return res.status(500).send("something wrong"); |
| 79 | + return res.status(200).send(admins); |
| 80 | +} |
| 81 | + |
| 82 | +const getSingleAdmin = async (req,res) => { |
| 83 | + const admin = await Admin.findById(req.params.adminId); |
| 84 | + if(!admin) return res.status(500).send("something wrong"); |
| 85 | + return res.status(200).send(admin); |
| 86 | +} |
| 87 | + |
| 88 | +const getMe = async (req,res) => { |
| 89 | + const admin = await Admin.findById(req.admin._id); |
| 90 | + if(!admin) return res.status(500).send("something wrong"); |
| 91 | + return res.status(200).send(admin); |
| 92 | +} |
| 93 | + |
| 94 | +const deleteAdmin = async (req,res) => { |
| 95 | + const admin = await Admin.findByIdAndDelete(req.params.adminId); |
| 96 | + if(!admin) return res.status(500).send("something wrong"); |
| 97 | + return res.status(200).send(admin); |
| 98 | +} |
| 99 | + |
| 100 | +module.exports = { |
| 101 | + register, |
| 102 | + getAllAdmin, |
| 103 | + getSingleAdmin, |
| 104 | + getMe, |
| 105 | + deleteAdmin, |
| 106 | + login, |
| 107 | + getUpdateAdmin |
| 108 | +} |
0 commit comments