-
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 pull request #39 from GSG-G10/37-auth-admin
create authentication to admin #37
- Loading branch information
Showing
4 changed files
with
31 additions
and
0 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 connection = require('../config/connection'); | ||
|
||
const checkAdminQuery = (email) => connection.query('SELECT * FROM admins WHERE email= ($1)', [email]); | ||
|
||
module.exports = checkAdminQuery; |
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,5 +1,7 @@ | ||
const isAuth = require('./isAuth'); | ||
const isAdmin = require('./isAdmin'); | ||
|
||
module.exports = { | ||
isAuth, | ||
isAdmin, | ||
}; |
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,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; |