-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* added role model * added middleware check for admin/user * added auth middleware & updated roles * updated email from header field * updated checkRoles * appened user info to headers for auth * moved middleware to seperate directory * fixed imports for checkrole * fixed authentication middleware * cleanup * removed all check role stuff * Update AuthenticationMiddleware.js * added routes from main
- Loading branch information
Showing
6 changed files
with
107 additions
and
7 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 |
---|---|---|
|
@@ -62,3 +62,4 @@ exports.delete = async (req, res) => { | |
await User.destroy({ where: { id } }); | ||
res.status(204).send(); | ||
}; | ||
|
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
const db = require('../models'); | ||
|
||
async function authMiddleware(req, res, next) { | ||
try { | ||
const utorid = req.headers.utorid; | ||
const http_mail = req.headers.http_mail; | ||
|
||
let user = await db.User.findOne({ where: { username: utorid } }); | ||
|
||
if (!user) { | ||
user = await db.User.create({ | ||
username: utorid, | ||
email: http_mail, | ||
role: 'user', | ||
}); | ||
} | ||
|
||
req.user = user; | ||
next(); | ||
} catch (error) { | ||
console.error('Error ensuring user exists:', error); | ||
res.status(500).send({ error: 'Internal Server Error' }); | ||
} | ||
} | ||
|
||
module.exports = authMiddleware; |
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,34 @@ | ||
function ErrorHandler(err, req, res, next) { | ||
console.error("Error", err); | ||
let code = 500; | ||
if (err instanceof HandledError) { | ||
code = err.code; | ||
} | ||
res.status(code).json({ | ||
error: err.message, | ||
}); | ||
} | ||
|
||
function AsyncWrapController(controller) { | ||
for (const key in controller) { | ||
if (typeof controller[key] === "function") { | ||
// console.log(`Changed ${key} to async.`); | ||
controller[key] = AsyncDecorator(controller[key]); | ||
} | ||
} | ||
} | ||
|
||
function AsyncDecorator(fn) { | ||
return (req, res, next) => { | ||
Promise.resolve(fn(req, res, next)).catch((e) => next(e)); | ||
}; | ||
} | ||
|
||
class HandledError extends Error { | ||
constructor(code, message) { | ||
super(message); | ||
this.code = code; | ||
} | ||
} | ||
|
||
module.exports = { ErrorHandler, AsyncWrapController, HandledError }; |
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,6 @@ | ||
function LoggingMiddleware(req, res, next) { | ||
console.log(`Requesting ${req.originalUrl} /w`, req.params, req.body); | ||
next(); | ||
} | ||
|
||
module.exports = LoggingMiddleware; |
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