-
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.
overhauling backend to use userId instead of _id
- Loading branch information
1 parent
0ff9376
commit 19e3249
Showing
12 changed files
with
140 additions
and
82 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,19 @@ | ||
const User = require('../models/User'); | ||
|
||
module.exports = async function isAdmin(req, res, next) { | ||
const isAdmin = async (req, res, next) => { | ||
try { | ||
const user = await User.findById(req.user.id); | ||
if (!user || !user.isAdmin) { | ||
return res | ||
.status(403) | ||
.json({ error: 'Access denied. Admin privileges required.' }); | ||
const userId = req.user.userId; // Ensure this is a number | ||
const user = await User.findOne({ userId: userId }); // Use findOne with userId | ||
|
||
if (user && user.isAdmin && (user.adminLevel === 'admin' || user.adminLevel === 'moderator')) { | ||
next(); | ||
} else { | ||
res.status(403).json({ error: 'Access denied. Admin privileges required.' }); | ||
} | ||
req.adminUser = user; | ||
next(); | ||
} catch (error) { | ||
console.error('Error checking admin status:', error); | ||
console.error('Error in isAdmin middleware:', error); | ||
res.status(500).json({ error: 'Internal server error' }); | ||
} | ||
}; | ||
|
||
module.exports = 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
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
Oops, something went wrong.