Skip to content

Commit

Permalink
Merge pull request #73 from kohkaixun/master
Browse files Browse the repository at this point in the history
Rectify admin view
  • Loading branch information
zzthian authored Nov 10, 2023
2 parents 337c910 + 5b1b2e3 commit db405cb
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
2 changes: 1 addition & 1 deletion backend/question-backend/routes/categoryRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { updateCategory } from "../controller/updateCategory.js";

const router = express.Router();

router.get("/", [checkLogin], getCategories);
router.get("/", getCategories);
router.post("/", [checkLogin, checkAdmin], addCategory);
router.put("/:oldName", [checkLogin, checkAdmin], updateCategory);
router.delete("/:name", [checkLogin, checkAdmin], deleteCategory);
Expand Down
25 changes: 25 additions & 0 deletions backend/user_profile_backend/middleware/validateAdmin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const axios = require('axios')
const { verifyJsonWebToken } = require('./tokenUtils')
const USER_HOST = process.env.USER_HOST ? process.env.USER_HOST : "http://localhost:4000/api/users"


async function validateAdmin (request, response, next) {
const token = request.headers.authorization
console.log(token)
try {
const is_admin = verifyJsonWebToken(token).user_data.is_admin
if (!is_admin) {
return response.status(401).json({ error: 'Unauthorised access. User not admin.' })
}
} catch (error) {
console.log("error")
console.log(error.message)
return response.status(401).json({ error: 'Unauthorised' })
}

next()
}

module.exports = {
validateAdmin
}
3 changes: 2 additions & 1 deletion backend/user_profile_backend/routes/user-profile-router.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const express = require('express')

const bodyParser = require('body-parser')
const { validateUser } = require('../middleware/validateUser')
const { validateAdmin } = require('../middleware/validateAdmin')

const getUserById = require('../controller/getUser').getUserById
const getUserByName = require('../controller/getUser').getUserByName
Expand Down Expand Up @@ -32,6 +33,6 @@ router.get('/userByName', [validateUser], getUserByName)
router.put('/updateUser', [validateUser], updateUserInfo)
router.delete('/deleteUser', [validateUser], deleteUserByUserID)
router.get('/checkUserAdmin', [validateUser], checkUSerAdmin)
router.put('/setUserAdmin', [validateUser], setUserAdmin)
router.put('/setUserAdmin', [validateUser, validateAdmin], setUserAdmin)

module.exports = router
2 changes: 1 addition & 1 deletion frontend/src/pages/AdminView.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export default function AdminView () {
// Make a PUT request to set the user as admin
const token = getAuthCookie()

axios.put(`${USER_HOST}/setUserAdmin?username=${username}`, {
axios.put(`${USER_HOST}/setUserAdmin?username=${username}`, {}, {
headers: {
'Authorization': token
}
Expand Down

0 comments on commit db405cb

Please sign in to comment.