Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const jwt = require('jsonwebtoken')
- const User = require('../models/User')
- module.exports = (req, res, next) => {
- if (!req.headers.authorization) {
- return res.status(401).end()
- }
- // get the last part from a authorization header string like "bearer token-value"
- const token = req.headers.authorization.split(' ')[1]
- // decode the token using a secret key-phrase
- return jwt.verify(token.toString(), 'somesupersecret', (err, decoded) => {
- if (err) {
- return res.status(401).end()
- }
- const userId = decoded.userId
- User
- .findOne({ _id: userId })
- .then(user => {
- if (!user) {
- return res.status(401).end()
- }
- req.user = user
- return next()
- })
- })
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement