-
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.
Merge branch 'development' into 37-auth-admin
- Loading branch information
Showing
11 changed files
with
90 additions
and
7 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 |
---|---|---|
@@ -1,8 +1,8 @@ | ||
const { userEstateshandler } = require('./users'); | ||
const { userEstateshandler, login } = require('./users'); | ||
const getAllUsers = require('./users/getAllUsers'); | ||
const { editEstate, deleteEstate } = require('./estates'); | ||
const logout = require('./logout'); | ||
|
||
module.exports = { | ||
getAllUsers, userEstateshandler, logout, deleteEstate, editEstate, | ||
getAllUsers, userEstateshandler, logout, login, deleteEstate, editEstate, | ||
}; |
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,9 +1,11 @@ | ||
const signup = require('./signup'); | ||
const userEstateshandler = require('./userEstates'); | ||
const getAllUsers = require('./getAllUsers'); | ||
const login = require('./login'); | ||
|
||
module.exports = { | ||
signup, | ||
getAllUsers, | ||
userEstateshandler, | ||
login, | ||
}; |
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,36 @@ | ||
/* eslint-disable no-console */ | ||
/* eslint-disable consistent-return */ | ||
const bcrypt = require('bcrypt'); | ||
const { loginSchema } = require('../../utils/validation/loginSchema'); | ||
const { checkEmailQuery } = require('../../database/quieres'); | ||
const { signToken } = require('../../utils'); | ||
|
||
const login = async (req, res, next) => { | ||
try { | ||
const { email, password } = req.body; | ||
await loginSchema.validateAsync(req.body); | ||
|
||
const { rows } = await checkEmailQuery(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 = login; |
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,6 +1,6 @@ | ||
-- | ||
INSERT INTO agents (name, email, password , phone) | ||
VALUES ('Kai', '[email protected]', '$2b$10$oNaAu46EHAyOCiufPgchaOQDq5opRxSFHB20m.e3wzDBlM5Yzztf2', '677-871-7450'), | ||
VALUES ('Kai', '[email protected]', '$2b$10$gT8Qb2Qe01W1QMRFmH9IC.3bmbA4PS2yG4XQvdkYWxKday.SbjGI2', '677-871-7450'), | ||
('Trixie', '[email protected]', '$2b$10$oNaAu46EHAyOCiufPgchaOQDq5opRxSFHB20m.e3wzDBlM5Yzztf2', '0599832685'), | ||
('Allina', '[email protected]', '$2b$10$oNaAu46EHAyOCiufPgchaOQDq5opRxSFHB20m.e3wzDBlM5Yzztf2', '630-385-8312'); | ||
|
||
|
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 connection = require('../config/connection'); | ||
|
||
const checkEmailQuery = (email) => connection.query('SELECT * FROM agents WHERE email= ($1)', [email]); | ||
|
||
module.exports = checkEmailQuery; |
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,13 +1,15 @@ | ||
const userEstatesQuery = require('./userEstatesQuiery'); | ||
const getAllUsersQuery = require('./getAllUsersQuery'); | ||
const checkAdminQuery = require('./checkAdmin'); | ||
const checkEmailQuery = require('./checkEmailQuery'); | ||
const editEstateQuery = require('./editEstatesQuery'); | ||
const deleteEstateQuery = require('./deleteEstateQuery'); | ||
|
||
module.exports = { | ||
getAllUsersQuery, | ||
userEstatesQuery, | ||
checkAdminQuery, | ||
checkEmailQuery, | ||
editEstateQuery, | ||
deleteEstateQuery, | ||
}; |
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,7 @@ | ||
const router = require('express').Router(); | ||
const { logout, login } = require('../controllers'); | ||
|
||
router.get('/logout', logout); | ||
router.post('/login', login); | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
const router = require('express').Router(); | ||
const { logout } = require('../controllers'); | ||
const auth = require('./auth'); | ||
const estate = require('./estate'); | ||
const users = require('./users'); | ||
|
||
router.use('/users', users); | ||
router.use('/estate', estate); | ||
router.get('/logout', logout); | ||
router.use('/', auth); | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ const connection = require('../database/config/connection'); | |
beforeEach(() => dbBuild()); | ||
afterAll(() => connection.end()); | ||
|
||
describe('Tests Server', () => { | ||
describe('Get all users', () => { | ||
test('get all users', async () => { | ||
const res = await supertest(app) | ||
.get('/api/v1/users') | ||
|
@@ -17,6 +17,30 @@ describe('Tests Server', () => { | |
}); | ||
}); | ||
|
||
describe('Tests login route', () => { | ||
test(' login route /login ', async () => { | ||
const res = await supertest(app) | ||
.post('/api/v1/login') | ||
.send({ | ||
email: '[email protected]', | ||
password: '12345', | ||
}) | ||
.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/login') | ||
.send({ | ||
email: '[email protected]', | ||
password: '123456987', | ||
}) | ||
.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) | ||
|
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,8 @@ | ||
const joi = require('joi'); | ||
|
||
const loginSchema = joi.object({ | ||
email: joi.string().email().required(), | ||
password: joi.string().min(5).required(), | ||
}); | ||
|
||
module.exports = { loginSchema }; |