Last active
August 15, 2021 16:57
-
-
Save richellyitalo/53c6d99715afafb4352b4569dd22be76 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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