-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added new authorization middleware to routes.
- Loading branch information
Marios Venetsianos
committed
Jan 24, 2023
1 parent
374bf5d
commit e10e26c
Showing
17 changed files
with
1,070 additions
and
823 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
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
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
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,48 +1,65 @@ | ||
const routes = require("express").Router(); | ||
let database = require("../services/database"); | ||
const bcrypt = require("bcrypt"); | ||
const { authUser, authAdmin } = require("../middleware/auth"); | ||
const { authUser, authAdmin, authAgency, authRole } = require("../middleware/auth"); | ||
|
||
routes.get("/", authUser, authAdmin, async function (req, res, next) { | ||
let latest_entry = await database.ministries.max("id").catch((error) => { | ||
console.log(error); | ||
}); // get entry with highest id | ||
const user = req.session.user; | ||
let res_data = await database.ministries | ||
.findOne({ where: { id: latest_entry } }) | ||
.catch((error) => { | ||
routes.get( | ||
"/", | ||
authUser, | ||
authRole, | ||
authAgency, | ||
authAdmin, | ||
async function (req, res, next) { | ||
let latest_entry = await database.ministries.max("id").catch((error) => { | ||
console.log(error); | ||
}); | ||
let ministries = []; | ||
for (i in res_data.dataValues.ministries) { | ||
let value = res_data.dataValues.ministries[i].ministry; | ||
if (value && String(value).trim()) { | ||
ministries.push({ ministry: value }); | ||
}); // get entry with highest id | ||
const user = req.session.user; | ||
let res_data = await database.ministries | ||
.findOne({ where: { id: latest_entry } }) | ||
.catch((error) => { | ||
console.log(error); | ||
}); | ||
let ministries = []; | ||
for (i in res_data.dataValues.ministries) { | ||
let value = res_data.dataValues.ministries[i].ministry; | ||
if (value && String(value).trim()) { | ||
ministries.push({ ministry: value }); | ||
} | ||
} | ||
res.render("user_views/create_user", { | ||
ministries: ministries, | ||
user: user, | ||
}); | ||
} | ||
res.render("user_views/create_user", { ministries: ministries, user:user }); | ||
}); | ||
); | ||
|
||
routes.post("/", authUser, authAdmin, async function (req, res, next) { | ||
const userPassword = req.body.password; | ||
bcrypt.hash(userPassword, 10, async function (err, hash) { | ||
//add row to user model, map values from req.body | ||
if (hash) { | ||
let res_data = await database.user.create({ | ||
fname: req.body.fname, | ||
lname: req.body.lname, | ||
taxId: req.body.taxId, | ||
username: req.body.username, | ||
password: hash, | ||
role: req.body.role, | ||
isAdmin: req.body.isAdmin, | ||
agency: req.body.ypoyrgeio, | ||
}); | ||
res.send(res_data); | ||
} else { | ||
console.log("error while hashing"); | ||
} | ||
}); | ||
}); | ||
routes.post( | ||
"/", | ||
authUser, | ||
authRole, | ||
authAgency, | ||
authAdmin, | ||
async function (req, res, next) { | ||
const userPassword = req.body.password; | ||
bcrypt.hash(userPassword, 10, async function (err, hash) { | ||
//add row to user model, map values from req.body | ||
if (hash) { | ||
let res_data = await database.user.create({ | ||
fname: req.body.fname, | ||
lname: req.body.lname, | ||
taxId: req.body.taxId, | ||
username: req.body.username, | ||
password: hash, | ||
role: req.body.role, | ||
isAdmin: req.body.isAdmin, | ||
agency: req.body.ypoyrgeio, | ||
}); | ||
res.send(res_data); | ||
} else { | ||
console.log("error while hashing"); | ||
} | ||
}); | ||
} | ||
); | ||
|
||
module.exports = routes; |
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,18 +1,18 @@ | ||
const routes = require('express').Router() | ||
const { authUser } = require('../middleware/auth'); | ||
const { authUser, authRole, authAgency } = require("../middleware/auth"); | ||
let database = require("../services/database") | ||
|
||
routes.get('/', authUser,async (req,res,next) =>{ | ||
let user = await database.user.findOne({ | ||
where: { | ||
taxId: req.session.user.taxId, | ||
}, | ||
}); | ||
if(user && user.dataValues){ | ||
res.render("user_views/dashboard",{user:user.dataValues}) | ||
}else{ | ||
res.status(404).send("Not found") | ||
} | ||
routes.get("/", authUser, authRole, authAgency, async (req, res, next) => { | ||
let user = await database.user.findOne({ | ||
where: { | ||
taxId: req.session.user.taxId, | ||
}, | ||
}); | ||
if (user && user.dataValues) { | ||
res.render("user_views/dashboard", { user: user.dataValues }); | ||
} else { | ||
res.status(404).send("Not found"); | ||
} | ||
}); | ||
|
||
module.exports = routes; |
Oops, something went wrong.