pune
pune

Reputation: 99

Using bearer authorization with Postman nodejs

Im fresher to nodejs developer But there is error in time of Authorization bearer I post some code and screenshot of code Please help me to solve my error

router/user.js Here i pass auth middleware, and writen code in middleware/awth.js

      router.get('/users', auth, async (req,res) => {
    try{
        const user = await User.find({})
        res.send(user)
    }catch(e){
        res.status(500).send()
    }  
})

middleware/auth.js Here i print token value in terminal but error occure

    const jwt = require('jsonwebtoken')
const User = require('../models/user')

const auth = async (req, res, next) => {
    try{
        const token = req.header('Authorization')
        console.log(token)        
    } catch(e) {
        res.status(401).send({ error: 'Please authenticate.' })
    }
}

module.exports = auth

*In postman i login with email and password ,it generate token *

enter image description here

**I paste that token in key and value pair in Header ** In key i written Authorization and in value i written Bearer space token value and send but not prceed any and not shwoing any token value in terminal

Thsis is showing after click send ...error occure

and i use latest version of postman 7+ Please help me to solve my error

Upvotes: 2

Views: 3099

Answers (1)

Darren Houston
Darren Houston

Reputation: 96

Instead of using your Headers tab, use the Authorization tab and select "Bearer Token" from the dropdown, then paste your token into the Token field.

Postman Authorization Token

In some version of Postman Authorization type 'Bearer Token' doesn't appear, In that case, you can pass a token in a header as below : enter image description here

Upvotes: 3

Related Questions