Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions middlewares/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,25 @@ const jwt = require('jsonwebtoken');
const config = require('config');

module.exports = (req, res, next) => {
const token = req.header('Authorization');
if (!token) {
const authHeader = req.header('Authorization');
if (!authHeader) {
const err = new Error('Access denied. No token provided.');
err.status = 401;
return next(err);
}

const parts = authHeader.split(' ');

if (!parts.length === 2) {
return res.status(401).send({ error: 'Token error' });
}

const [scheme, token] = parts;

if (!/^Bearer$/i.test(scheme)) {
return res.status(401).send({ error: 'Token Malformatted' });
}

try {
const decoded = jwt.verify(token, config.get('jwtPrivateKey'));
req.user = decoded;
Expand Down
2 changes: 1 addition & 1 deletion modules/tokenGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const jwt = require('jsonwebtoken');
const config = require('config');

function sendToken (req) {
const token = jwt.sign({ username: req.body.username }, config.get('jwtPrivateKey'), { expiresIn: '7 days' });
const token = jwt.sign({ username: req.body.username }, config.get('jwtPrivateKey'), { expiresIn: 604800 });
Comment thread
amintasvrp marked this conversation as resolved.
Outdated
return token;
}

Expand Down