Skip to content

Commit

Permalink
Hotfixes all the way down
Browse files Browse the repository at this point in the history
  • Loading branch information
Ratstail91 committed Dec 23, 2023
1 parent 8ab786b commit 288e584
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 5 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "auth-server",
"version": "1.8.2",
"version": "1.8.3",
"description": "An API centric auth server. Uses Sequelize and mariaDB by default.",
"main": "server/server.js",
"scripts": {
Expand Down
6 changes: 5 additions & 1 deletion server/auth/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const { accounts } = require('../database/models');

//middleware
const tokenAuth = require('../utilities/token-auth');
const tokenDecode = require('../utilities/token-decode');

//signup -> validate -> login all without a token
router.post('/signup', require('./signup'));
Expand All @@ -20,7 +21,7 @@ router.patch('/reset', require('./password-reset'));
router.delete('/logout', require('./logout'));

//authenticate token
router.use(tokenAuth);
router.use(tokenDecode);

//middleware
router.use(async (req, res, next) => {
Expand All @@ -44,6 +45,9 @@ router.use(async (req, res, next) => {
//refresh token
router.post('/token', require('./token'));

//authenticate token
router.use(tokenAuth);

//basic account management (needs a token)
router.get('/account', require('./account-query'));
router.patch('/account', require('./account-update'));
Expand Down
2 changes: 1 addition & 1 deletion server/utilities/token-auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = (req, res, next) => {
const accessToken = authHeader?.split(' ')[1]; //'Bearer token'

if (!accessToken) {
return res.status(401).send('No access token found');
return res.status(401).send('No access token provided');
}

return jwt.verify(accessToken, process.env.SECRET_ACCESS, (err, user) => {
Expand Down
21 changes: 21 additions & 0 deletions server/utilities/token-decode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const jwt = require('jsonwebtoken');

//middleware to decode the JWT token
module.exports = (req, res, next) => {
const authHeader = req.headers['authorization'];
const accessToken = authHeader?.split(' ')[1]; //'Bearer token'

if (!accessToken) {
return res.status(401).send('No access token provided');
}

return jwt.decode(accessToken, process.env.SECRET_ACCESS, (err, user) => {
if (err) {
return res.status(403).send(err);
}

req.user = user;

return next();
});
};

0 comments on commit 288e584

Please sign in to comment.