-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
mohammedsalah7
committed
Oct 31, 2021
1 parent
1b876bf
commit 4a4a5d1
Showing
6 changed files
with
70 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const adminLogin = require('./login'); | ||
|
||
module.exports = { | ||
adminLogin, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
|