Skip to content

Commit

Permalink
Merge pull request #65 from GSG-G10/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
Mu7ammadAbed authored Nov 1, 2021
2 parents badf1bd + 1b876bf commit a51f8ca
Show file tree
Hide file tree
Showing 27 changed files with 483 additions and 56 deletions.
4 changes: 2 additions & 2 deletions server/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ if (NODE_ENV === 'production') {
res.sendFile(join(__dirname, '..', 'client', 'build', 'index.html'));
});
}
router.use(notFoundError);
router.use(serverError);
app.use(notFoundError);
app.use(serverError);

module.exports = app;
24 changes: 24 additions & 0 deletions server/controllers/estates/deleteEstate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const { deleteEstateQuery } = require('../../database/quieres');

module.exports = async (req, res, next) => {
const { estateId } = req.params;
// check is number
if (!(estateId > 0)) {
return res.status(400).json({
message: 'Invalid estate id',
});
}
try {
const { rowCount } = await deleteEstateQuery(estateId);
if (rowCount > 0) {
return res.json({
message: 'Estate deleted successfully',
});
}
return res.status(400).json({
message: 'You can\'t complete this process at the moment',
});
} catch (err) {
return next(err);
}
};
31 changes: 31 additions & 0 deletions server/controllers/estates/editEstate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const { editEstateQuery } = require('../../database/quieres');
const { editEstateValidation } = require('../../utils/validation/editEstateValidation');

const editEstate = async (req, res, next) => {
try {
const {
estateId, title, price, description,
type, category, street, city, region,
bathrooms, bedrooms, rooms, space, available,
} = await editEstateValidation.validateAsync({ ...req.body, ...req.params });

const { rowCount } = await editEstateQuery(
estateId, title, price, description, type,
category, street, city, region, bathrooms,
bedrooms, rooms, space, available,
);

if (rowCount === 0) {
res.status(400).json({ status: 400, message: 'enter valid estate id ' });
} else {
res.json({ message: 'Estate updated successfully' });
}
} catch (err) {
if (err.details) {
res.status(400).json({ status: 400, message: err.details.message });
} else {
next(err);
}
}
};
module.exports = editEstate;
7 changes: 7 additions & 0 deletions server/controllers/estates/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const editEstate = require('./editEstate');
const deleteEstate = require('./deleteEstate');

module.exports = {
editEstate,
deleteEstate,
};
7 changes: 5 additions & 2 deletions server/controllers/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +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 };
module.exports = {
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
9 changes: 8 additions & 1 deletion server/controllers/users/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
const signup = require('./signup');
const userEstateshandler = require('./userEstates');
const getAllUsers = require('./getAllUsers');
const login = require('./login');

module.exports = { getAllUsers, userEstateshandler };
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;
24 changes: 24 additions & 0 deletions server/controllers/users/signup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const { hash } = require('bcrypt');
const agentSchema = require('../../utils/validation/agentSchema');
const signUpQuery = require('../../database/quieres/account/signUp');
const { signToken } = require('../../utils');

module.exports = async (req, res, next) => {
try {
const {
error, value: {
password, email, username, phone,
},
} = agentSchema.validate(req.body);
if (error) return res.status(400).json({ message: error.details[0].message });
const hasedPasword = await hash(password, 10);
await signUpQuery(username, email, phone, hasedPasword);
const token = await signToken(email, username, phone);
return res.status(201).cookie('token', token).json({ message: 'user created' });
} catch (err) {
if (err.code === '23505') {
return res.status(400).json({ message: 'The user is already exists' });
}
return next(err);
}
};
3 changes: 2 additions & 1 deletion server/database/config/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ const { Pool } = require('pg');

const {
NODE_ENV, DATABASE_URL_DEV, DATABASE_URL, DATABASE_URL_TEST,

} = process.env;
let dbUrl = '';
switch (NODE_ENV) {
case 'development':
dbUrl = DATABASE_URL_DEV;

break;
case 'production':
dbUrl = DATABASE_URL;
Expand All @@ -23,5 +25,4 @@ const options = {
connectionString: dbUrl,
ssl: { rejectUnauthorized: false },
};

module.exports = new Pool(options);
62 changes: 22 additions & 40 deletions server/database/config/fakeData.sql
Original file line number Diff line number Diff line change
@@ -1,43 +1,25 @@
insert into admins (username, password , email) values ('ameera', 'F1A1CC71ED10594F97B1B6CF94A00727', '[email protected]');
insert into admins (username, password , email) values ('haroon', 'F1A1CC71ED10594F97B1B6CF94A00727', '[email protected]');
insert into admins (username, password , email) values ('elham', 'F1A1CC71ED10594F97B1B6CF94A00727', '[email protected]');
insert into admins (username, password , email) values ('sallah', 'F1A1CC71ED10594F97B1B6CF94A00727', '[email protected]');
insert into agents (name, email, password , phone, avater) values ('Kai', '[email protected]', '6D29E04CD937DC37CB72E42736CA238C', '481-649-8020', 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSrVnja3DFheGQjch5AL1n0Rk8nOFHm6Ny60w&usqp=CAU');
insert into agents (name, email, password , phone, avater) values ('Trixie', '[email protected]', '6D29E04CD937DC37CB72E42736CA238C', '663-871-7450', 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSrVnja3DFheGQjch5AL1n0Rk8nOFHm6Ny60w&usqp=CAU');
insert into agents (name, email, password , phone, avater) values ('Allina', '[email protected]', '6D29E04CD937DC37CB72E42736CA238C', '630-385-8312', 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSrVnja3DFheGQjch5AL1n0Rk8nOFHm6Ny60w&usqp=CAU');
insert into agents (name, email, password , phone, avater) values ('Maye', '[email protected]', '6D29E04CD937DC37CB72E42736CA238C', '210-886-2847', 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSrVnja3DFheGQjch5AL1n0Rk8nOFHm6Ny60w&usqp=CAU');
insert into agents (name, email, password , phone, avater) values ('Lillian', '[email protected]', '6D29E04CD937DC37CB72E42736CA238C', '270-209-1221', 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSrVnja3DFheGQjch5AL1n0Rk8nOFHm6Ny60w&usqp=CAU');
insert into agents (name, email, password , phone, avater) values ('Emlyn', '[email protected]', '6D29E04CD937DC37CB72E42736CA238C', '426-753-9984', 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSrVnja3DFheGQjch5AL1n0Rk8nOFHm6Ny60w&usqp=CAU');
insert into estates ( agent_id, title, price, description, type, category, street, city, region, bathrooms, bedrooms, rooms, space, approved, rate, available) values (1, 'suscipit ligula in', '190483.22', 'tristique', 'Buy', 'House', '3152 Morningstar Park', 'Edinburgh of the Seven Seas', 'Saint Helena', 2, 1, 1, 194, false, 2, false);
insert into estates ( agent_id, title, price, description, type, category, street, city, region, bathrooms, bedrooms, rooms, space, approved, rate, available) values (2, 'ipsum primis in', '242471.89', 'est quam pharetra magna ac consequat metus sapien ut nunc vestibulum ante', 'Buy', 'House', '100 Butternut Hill', 'Bern', 'Switzerland', 1, 2, 2, 226, true, 5, true);
insert into estates ( agent_id, title, price, description, type, category, street, city, region, bathrooms, bedrooms, rooms, space, approved, rate, available) values (3, 'rhoncus sed vestibulum', '84726.00', 'massa volutpat convallis morbi odio odio elementum eu interdum eu tincidunt in', 'Buy', 'House', '0824 Mcguire Way', 'Kungshamn', 'Sweden', 1, 3, 3, 235, true, 5, false);
insert into estates ( agent_id, title, price, description, type, category, street, city, region, bathrooms, bedrooms, rooms, space, approved, rate, available) values (4, 'sociis natoque penatibus et', '106226.71', 'quis odio consequat', 'Buy', 'House', '15918 Mcguire Point', 'Ranong', 'Thailand', 2, 4, 4, 244, true, 1, false);
insert into estates ( agent_id, title, price, description, type, category, street, city, region, bathrooms, bedrooms, rooms, space, approved, rate, available) values (5, 'in faucibus orci luctus', '116162.27', 'parturient montes nascetur ridiculus mus vivamus vestibulum sagittis sapien cum', 'Buy', 'House', '898 Dixon Crossing', 'Gelap', 'Indonesia', 3, 2, 4, 150, false, 3, false);
insert into estates ( agent_id, title, price, description, type, category, street, city, region, bathrooms, bedrooms, rooms, space, approved, rate, available) values (6, 'vestibulum ante ipsum primis', '194193.55', 'leo odio porttitor id consequat in consequat ut nulla sed', 'Buy', 'House', '0891 7th Park', 'Álimos', 'Greece', 1, 3, 2, 174, false, 1, false);
insert into images ( estate_id, image) values (1,'https://images.unsplash.com/photo-1580587771525-78b9dba3b914?ixid=MnwxMjA3fDB8MHxzZWFyY2h8MXx8bHV4dXJ5JTIwaG91c2V8ZW58MHx8MHx8&ixlib=rb-1.2.1&w=1000&q=80');
insert into images ( estate_id, image) values (1,'https://archello.s3.eu-central-1.amazonaws.com/images/2018/05/17/1.tobiarchitects.1526566679.5654.jpg');
insert into images ( estate_id, image) values (1,'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRHpF_yr6jMER79FRSejAszXCsyd__87nEF6g&usqp=CAU');
insert into images ( estate_id, image) values (1,'https://images.unsplash.com/photo-1580587771525-78b9dba3b914?ixid=MnwxMjA3fDB8MHxzZWFyY2h8MXx8bHV4dXJ5JTIwaG91c2V8ZW58MHx8MHx8&ixlib=rb-1.2.1&w=1000&q=80');
--
INSERT INTO agents (name, email, password , phone)
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');

insert into images ( estate_id, image) values (2,'https://cdn.vox-cdn.com/thumbor/6itLJS9BZ-B5gXPjM1AB_z-ZKVI=/0x0:3000x2000/1200x800/filters:focal(1260x760:1740x1240)/cdn.vox-cdn.com/uploads/chorus_image/image/65890203/iStock_1067331614.7.jpg');
insert into images ( estate_id, image) values (2,'https://archello.s3.eu-central-1.amazonaws.com/images/2018/05/17/1.tobiarchitects.1526566679.5654.jpg');
insert into images ( estate_id, image) values (2,'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRHpF_yr6jMER79FRSejAszXCsyd__87nEF6g&usqp=CAU');
insert into images ( estate_id, image) values (2,'https://images.unsplash.com/photo-1580587771525-78b9dba3b914?ixid=MnwxMjA3fDB8MHxzZWFyY2h8MXx8bHV4dXJ5JTIwaG91c2V8ZW58MHx8MHx8&ixlib=rb-1.2.1&w=1000&q=80');
INSERT INTO admins (username, password , email) VALUES ('ameera', '$2b$10$oNaAu46EHAyOCiufPgchaOQDq5opRxSFHB20m.e3wzDBlM5Yzztf2', 'ameera2021abed@gmail.com'),
('haroon', '$2b$10$oNaAu46EHAyOCiufPgchaOQDq5opRxSFHB20m.e3wzDBlM5Yzztf2', 'hro19502001@gmail.com'),
('elham', '$2b$10$oNaAu46EHAyOCiufPgchaOQDq5opRxSFHB20m.e3wzDBlM5Yzztf2', 'elham2000fadel@gmail.com'),
('sallah', '$2b$10$oNaAu46EHAyOCiufPgchaOQDq5opRxSFHB20m.e3wzDBlM5Yzztf2', 'mohmsal96@gmail.com');

insert into images ( estate_id, image) values (3,'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSh4O9GCySQw_9C24XfInhq-lYgfnHlRSMB5g&usqp=CAU');
insert into images ( estate_id, image) values (3,'https://archello.s3.eu-central-1.amazonaws.com/images/2018/05/17/1.tobiarchitects.1526566679.5654.jpg');
insert into images ( estate_id, image) values (3,'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRHpF_yr6jMER79FRSejAszXCsyd__87nEF6g&usqp=CAU');
insert into images ( estate_id, image) values (3,'https://images.unsplash.com/photo-1580587771525-78b9dba3b914?ixid=MnwxMjA3fDB8MHxzZWFyY2h8MXx8bHV4dXJ5JTIwaG91c2V8ZW58MHx8MHx8&ixlib=rb-1.2.1&w=1000&q=80');
insert into images ( estate_id, image) values (4,'https://images.unsplash.com/photo-1580587771525-78b9dba3b914?ixid=MnwxMjA3fDB8MHxzZWFyY2h8MXx8bHV4dXJ5JTIwaG91c2V8ZW58MHx8MHx8&ixlib=rb-1.2.1&w=1000&q=80');
insert into images ( estate_id, image) values (4,'https://archello.s3.eu-central-1.amazonaws.com/images/2018/05/17/1.tobiarchitects.1526566679.5654.jpg');
insert into images ( estate_id, image) values (4,'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRHpF_yr6jMER79FRSejAszXCsyd__87nEF6g&usqp=CAU');
insert into images ( estate_id, image) values (4,'https://images.unsplash.com/photo-1580587771525-78b9dba3b914?ixid=MnwxMjA3fDB8MHxzZWFyY2h8MXx8bHV4dXJ5JTIwaG91c2V8ZW58MHx8MHx8&ixlib=rb-1.2.1&w=1000&q=80');
INSERT INTO estates ( agent_id, title, price, description, type, category, street, city, region, bathrooms, bedrooms, rooms, space, approved, rate, available) VALUES (1, 'suscipit ligula in', 190483.22, 'tristique', 'Buy', 'House', '3152 Morningstar Park', 'Edinburgh of the Seven Seas', 'Saint Helena', 2, 1, 1, 194, false, 2, false),
(2, 'ipsum primis in', 242471.89, 'est quam pharetra magna ac consequat metus sapien ut nunc vestibulum ante', 'Buy', 'House', '100 Butternut Hill', 'Bern', 'Switzerland', 1, 2, 2, 226, true, 5, true),
(3, 'rhoncus sed vestibulum', 84726.00, 'massa volutpat convallis morbi odio odio elementum eu interdum eu tincidunt in', 'Buy', 'House', '0824 Mcguire Way', 'Kungshamn', 'Sweden', 1, 3, 3, 235, true, 5, false),
(1, 'sociis natoque penatibus et', 106226.71, 'quis odio consequat', 'Buy', 'House', '15918 Mcguire Point', 'Ranong', 'Thailand', 2, 4, 4, 244, true, 1, false),
(2, 'in faucibus orci luctus', 116162.27, 'parturient montes nascetur ridiculus mus vivamus vestibulum sagittis sapien cum', 'Buy', 'House', '898 Dixon Crossing', 'Gelap', 'Indonesia', 3, 2, 4, 150, false, 3, false),
(3, 'vestibulum ante ipsum primis', 194193.55, 'leo odio porttitor id consequat in consequat ut nulla sed', 'Buy', 'House', '0891 7th Park', 'Álimos', 'Greece', 1, 3, 2, 174, false, 1, false);

insert into images ( estate_id, image) values (5,'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTsj7rODPg-8QIdi6VONhE84ZFng3D5WpNfbA&usqp=CAU');
insert into images ( estate_id, image) values (5,'https://archello.s3.eu-central-1.amazonaws.com/images/2018/05/17/1.tobiarchitects.1526566679.5654.jpg');
insert into images ( estate_id, image) values (5,'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRHpF_yr6jMER79FRSejAszXCsyd__87nEF6g&usqp=CAU');
insert into images ( estate_id, image) values (5,'https://images.unsplash.com/photo-1580587771525-78b9dba3b914?ixid=MnwxMjA3fDB8MHxzZWFyY2h8MXx8bHV4dXJ5JTIwaG91c2V8ZW58MHx8MHx8&ixlib=rb-1.2.1&w=1000&q=80');
insert into images ( estate_id, image) values (6,'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSBOKk95d1JPgPWHYPk_Z2NXd_ntTr-N0E0yQ&usqp=CAU');
insert into images ( estate_id, image) values (6,'https://archello.s3.eu-central-1.amazonaws.com/images/2018/05/17/1.tobiarchitects.1526566679.5654.jpg');
insert into images ( estate_id, image) values (6,'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRHpF_yr6jMER79FRSejAszXCsyd__87nEF6g&usqp=CAU');
insert into images ( estate_id, image) values (6,'https://images.unsplash.com/photo-1580587771525-78b9dba3b914?ixid=MnwxMjA3fDB8MHxzZWFyY2h8MXx8bHV4dXJ5JTIwaG91c2V8ZW58MHx8MHx8&ixlib=rb-1.2.1&w=1000&q=80');
INSERT INTO images ( estate_id, image) VALUES (1,'https://archello.s3.eu-central-1.amazonaws.com/images/2018/05/17/1.tobiarchitects.1526566679.5654.jpg'),
(2,'https://archello.s3.eu-central-1.amazonaws.com/images/2018/05/17/2.tobiarchitects.1526566679.5654.jpg'),
(3,'https://archello.s3.eu-central-1.amazonaws.com/images/2018/05/17/3.tobiarchitects.1526566679.5654.jpg'),
(1,'https://archello.s3.eu-central-1.amazonaws.com/images/2018/05/17/4.tobiarchitects.1526566679.5654.jpg'),
(2,'https://archello.s3.eu-central-1.amazonaws.com/images/2018/05/17/5.tobiarchitects.1526566679.5654.jpg'),
(3,'https://archello.s3.eu-central-1.amazonaws.com/images/2018/05/17/6.tobiarchitects.1526566679.5654.jpg');

3 changes: 3 additions & 0 deletions server/database/quieres/account/signUp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const connection = require('../../config/connection');

module.exports = (userName, email, phone, password) => connection.query('INSERT INTO agents (name,email,phone,password) VALUES ($1,$2,$3,$4)', [userName, email, phone, password]);
5 changes: 5 additions & 0 deletions server/database/quieres/checkAdmin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const connection = require('../config/connection');

const checkAdminQuery = (email) => connection.query('SELECT * FROM admins WHERE email= ($1)', [email]);

module.exports = checkAdminQuery;
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;
3 changes: 3 additions & 0 deletions server/database/quieres/deleteEstateQuery.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const connection = require('../config/connection');

module.exports = (id) => connection.query('DELETE FROM estates WHERE id = $1', [id]);
12 changes: 12 additions & 0 deletions server/database/quieres/editEstatesQuery.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const connection = require('../config/connection');

const editEstateQuery = (id, title, price, description, type, category, street,
city, region, bathrooms, bedrooms, rooms, space, available) => (
connection.query(
`UPDATE estates SET title = $1, price = $2, description = $3, type = $4,
category = $5, street = $6, city = $7, region = $8, bathrooms = $9,
bedrooms = $10, rooms = $11, space = $12, available = $13 WHERE id = $14;`,
[title, price, description, type, category, street, city,
region, bathrooms, bedrooms, rooms, space, available, id],
));
module.exports = editEstateQuery;
9 changes: 8 additions & 1 deletion server/database/quieres/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +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,
};
2 changes: 2 additions & 0 deletions server/middleware/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const isAuth = require('./isAuth');
const isAdmin = require('./isAdmin');

module.exports = {
isAuth,
isAdmin,
};
22 changes: 22 additions & 0 deletions server/middleware/isAdmin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const { checkAdminQuery } = require('../database/quieres');
const { verifyToken } = require('../utils');

const isAdmin = async (req, res, next) => {
try {
const { token } = req.cookies;
if (!token) {
return res.status(400).json({ message: 'You are not authorized' });
}
const decoded = await verifyToken(token);
req.email = decoded.email;

const rows = await checkAdminQuery(req.email);
if (!rows.length) {
return res.status(400).json({ message: 'You are not authorized' });
}
return next();
} catch (err) {
return next(err);
}
};
module.exports = isAdmin;
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;
9 changes: 9 additions & 0 deletions server/routes/estate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const router = require('express').Router();

// const { isAuth, isAdmin } = require('../middleware');
const { editEstate, deleteEstate } = require('../controllers');

router.put('/:estateId', editEstate);

router.delete('/:estateId', deleteEstate);
module.exports = router;
Loading

0 comments on commit a51f8ca

Please sign in to comment.