Skip to content

Commit

Permalink
Merge branch 'development' into 37-auth-admin
Browse files Browse the repository at this point in the history
  • Loading branch information
Mu7ammadAbed authored Oct 31, 2021
2 parents 6a06519 + 3f896e8 commit c297bf0
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 7 deletions.
4 changes: 2 additions & 2 deletions server/controllers/index.js
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,
};
1 change: 0 additions & 1 deletion server/controllers/users/getAllUsers.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable no-console */
const { getAllUsersQuery } = require('../../database/quieres');

const getAllUsers = async (req, res, next) => {
Expand Down
2 changes: 2 additions & 0 deletions server/controllers/users/index.js
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,
};
36 changes: 36 additions & 0 deletions server/controllers/users/login.js
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;
2 changes: 1 addition & 1 deletion server/database/config/fakeData.sql
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');

Expand Down
5 changes: 5 additions & 0 deletions server/database/quieres/checkEmailQuery.js
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;
2 changes: 2 additions & 0 deletions server/database/quieres/index.js
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,
};
7 changes: 7 additions & 0 deletions server/routes/auth.js
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;
4 changes: 2 additions & 2 deletions server/routes/index.js
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;
26 changes: 25 additions & 1 deletion server/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions server/utils/validation/loginSchema.js
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 };

0 comments on commit c297bf0

Please sign in to comment.