Skip to content

Commit

Permalink
create end point to login admin #69
Browse files Browse the repository at this point in the history
  • Loading branch information
mohammedsalah7 committed Oct 31, 2021
1 parent 1b876bf commit 4a4a5d1
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 3 deletions.
5 changes: 5 additions & 0 deletions server/controllers/admins/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const adminLogin = require('./login');

module.exports = {
adminLogin,
};
35 changes: 35 additions & 0 deletions server/controllers/admins/login.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/* eslint-disable consistent-return */
const bcrypt = require('bcrypt');
const { loginSchema } = require('../../utils/validation/loginSchema');
const { checkAdminQuery } = require('../../database/quieres');
const { signToken } = require('../../utils');

const adminLogin = async (req, res, next) => {
try {
const { email, password } = req.body;
await loginSchema.validateAsync(req.body);

const { rows } = await checkAdminQuery(email);

if (!rows.length) {
return res.status(400).json({ message: 'Invalid email or password' });
}

const compared = await bcrypt.compare(password, rows[0].password);
if (!compared) {
return res.status(400).json({ message: 'Invalid email or password' });
}
const token = await signToken(email, rows[0].id);
return res.cookie('token', token).json({ message: 'You are Logged Successfully' });
} catch (err) {
if (err.details) {
res.status(400).json({
message: err.details[0].message,
});
} else {
return next(err);
}
}
};

module.exports = adminLogin;
4 changes: 3 additions & 1 deletion server/controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ const { userEstateshandler, login } = require('./users');
const getAllUsers = require('./users/getAllUsers');
const { editEstate, deleteEstate } = require('./estates');
const logout = require('./logout');
const adminLogin = require('./admins');

module.exports = {
getAllUsers, userEstateshandler, logout, login, deleteEstate, editEstate,
getAllUsers, userEstateshandler, logout, login, deleteEstate, editEstate, adminLogin,

};
3 changes: 2 additions & 1 deletion server/routes/auth.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const router = require('express').Router();
const { logout, login } = require('../controllers');
const { logout, login, adminLogin } = require('../controllers');

router.get('/logout', logout);
router.post('/login', login);
router.post('/adminLogin', adminLogin);

module.exports = router;
2 changes: 1 addition & 1 deletion server/routes/users.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const router = require('express').Router();
const { userEstateshandler, getAllUsers, logout } = require('../controllers');
const { userEstateshandler, getAllUsers } = require('../controllers');
const estate = require('./estate');
const signup = require('../controllers/users/signup');

Expand Down
24 changes: 24 additions & 0 deletions server/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,30 @@ describe('Tests login route', () => {
});
});

describe('Tests login route to admin', () => {
test(' login route /login ', async () => {
const res = await supertest(app)
.post('/api/v1/adminLogin')
.send({
email: '[email protected]',
password: '123456',
})
.expect(200);
return expect(res.body).toEqual({ message: 'You are Logged Successfully' });
});

test(' login route /login with error in email or password ', async () => {
const res = await supertest(app)
.post('/api/v1/adminLogin')
.send({
email: '[email protected]',
password: '1234566',
})
.expect(400);
return expect(res.body).toEqual({ message: 'Invalid email or password' });
});
});

describe('user estates', () => {
test('get users estates', async () => {
const res = await supertest(app)
Expand Down

0 comments on commit 4a4a5d1

Please sign in to comment.