Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create authentication to admin #37 #39

Merged
merged 5 commits into from
Oct 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
2 changes: 2 additions & 0 deletions server/database/quieres/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
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,
Expand Down
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;