Skip to content

Instantly share code, notes, and snippets.

@richellyitalo
Last active August 15, 2021 16:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save richellyitalo/53c6d99715afafb4352b4569dd22be76 to your computer and use it in GitHub Desktop.
Save richellyitalo/53c6d99715afafb4352b4569dd22be76 to your computer and use it in GitHub Desktop.
import { promisify } from 'util';
import jwt from 'jsonwebtoken';
import authConfig from '../../config/auth';
export default async (req, res, next) => {
const authHeader = req.headers.authorization;
if (!authHeader) {
return res.status(401).json({ error: 'Não autenticado!' });
}
const [, token] = authHeader.split(' ');
try {
const decoded = await promisify(jwt.verify)(token, authConfig.secret);
req.userId = decoded.id;
// const verify = promisify(jwt.verify); // cria função promise
// const decoded = verify(token, authConfig.secret)
// .then(res1 => console.log('deu bom', res1))
// .catch(err => console.log('deu ruim', err));
} catch (err) {
// console.log(err.message);
return res.status(401).json({ error: 'Token inválido' });
}
return next();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment