Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 61ddd5b38f | |||
| cbd3ed9d3e | |||
| 7bbd6bbcf1 | |||
| 7ddef6ed1b |
@@ -77,4 +77,13 @@ Content-Type: application/json
|
|||||||
"password": "helloworld"
|
"password": "helloworld"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//DOCS: Sets the privilege of the specified user; usable only by admins
|
||||||
|
DELETE /auth/deletion
|
||||||
|
Authorization: Bearer accessToken
|
||||||
|
Content-Type: application/json
|
||||||
|
|
||||||
|
{
|
||||||
|
"username": "example",
|
||||||
|
"privilege: "administrator"
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -0,0 +1,25 @@
|
|||||||
|
const { accounts } = require('../database/models');
|
||||||
|
|
||||||
|
//auth/account/privilege
|
||||||
|
const route = async (req, res) => {
|
||||||
|
//check the user's privilege
|
||||||
|
if (req.user.privilege != 'administrator') {
|
||||||
|
return res.status(401).send('Only admins can change privilege');
|
||||||
|
}
|
||||||
|
|
||||||
|
const updated = await accounts.update({
|
||||||
|
privilege: req.body.privilege
|
||||||
|
}, {
|
||||||
|
where: {
|
||||||
|
username: req.body.username
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (updated < 1) {
|
||||||
|
return res.status(403).send(`Unknown account`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return res.status(200).end();
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = route;
|
||||||
@@ -9,12 +9,12 @@ const route = async (req, res) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
if (!account) {
|
if (!account) {
|
||||||
res.status(401).send('Unknown account');
|
return res.status(401).send('Unknown account');
|
||||||
}
|
}
|
||||||
|
|
||||||
//respond with the private-facing data
|
//respond with the private-facing data
|
||||||
res.status(200).json({
|
return res.status(200).json({
|
||||||
contact: await account.contact
|
contact: account.contact
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ const express = require('express');
|
|||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
|
|
||||||
//middleware
|
//middleware
|
||||||
const authToken = require('../utilities/token-auth');
|
const tokenAuth = require('../utilities/token-auth');
|
||||||
|
|
||||||
//signup -> validate -> login all without a token
|
//signup -> validate -> login all without a token
|
||||||
router.post('/signup', require('./signup'));
|
router.post('/signup', require('./signup'));
|
||||||
@@ -13,7 +13,7 @@ router.post('/login', require('./login'));
|
|||||||
router.post('/token', require('./token'));
|
router.post('/token', require('./token'));
|
||||||
|
|
||||||
//middleware
|
//middleware
|
||||||
router.use(authToken);
|
router.use(tokenAuth);
|
||||||
|
|
||||||
//basic account management (needs a token)
|
//basic account management (needs a token)
|
||||||
router.delete('/logout', require('./logout'));
|
router.delete('/logout', require('./logout'));
|
||||||
@@ -21,4 +21,6 @@ router.get('/account', require('./account'));
|
|||||||
router.patch('/update', require('./update'));
|
router.patch('/update', require('./update'));
|
||||||
router.delete('/deletion', require('./deletion'));
|
router.delete('/deletion', require('./deletion'));
|
||||||
|
|
||||||
|
router.patch('/account/privilege', require('./account-privilege'));
|
||||||
|
|
||||||
module.exports = router;
|
module.exports = router;
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ const sequelize = new Sequelize(process.env.DB_DATABASE, process.env.DB_USERNAME
|
|||||||
host: process.env.DB_HOSTNAME,
|
host: process.env.DB_HOSTNAME,
|
||||||
dialect: 'mariadb',
|
dialect: 'mariadb',
|
||||||
timezone: process.env.DB_TIMEZONE,
|
timezone: process.env.DB_TIMEZONE,
|
||||||
logging: false
|
logging: process.env.DB_LOGGING ? console.log : false
|
||||||
});
|
});
|
||||||
|
|
||||||
sequelize.sync();
|
sequelize.sync();
|
||||||
|
|||||||
@@ -6,16 +6,16 @@ module.exports = (req, res, next) => {
|
|||||||
const token = authHeader?.split (' ')[1]; //'Bearer token'
|
const token = authHeader?.split (' ')[1]; //'Bearer token'
|
||||||
|
|
||||||
if (!token) {
|
if (!token) {
|
||||||
return res.status(401).end();
|
return res.status(401).send('No token found');
|
||||||
}
|
}
|
||||||
|
|
||||||
jwt.verify(token, process.env.SECRET_ACCESS, (err, user) => {
|
return jwt.verify(token, process.env.SECRET_ACCESS, (err, user) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
return res.status(403).end();
|
return res.status(403).send(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
req.user = user;
|
req.user = user;
|
||||||
|
|
||||||
next();
|
return next();
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
Reference in New Issue
Block a user