Skip to content Skip to sidebar Skip to footer

Get Information From User In Database

I am making full stack app and learn from tutorials and videos . I have a problem with GET request to get information about user which is login in the system. I use Postman to chec

Solution 1:

For your middle ware for getting the token (auth function)

const { authorization } = req.headersif (!authorization) {
        console.log('[No Authorization Code]'); 
        return res.status(401).send({ message: 'Unauthorized' });
    }
        

    if (!authorization.startsWith('Bearer')) {
        console.log('[Authorization need to start with Bearer]')
        return res.status(401).send({ message: 'Unauthorized' });
    }

    const split = authorization.split('Bearer ')
    if (split.length !== 2) {
        console.log('[Invalid Authorization Param')
        return res.status(401).send({ message: 'Unauthorized' });
    }

     const token = split[1]  //this is your token to use with jwt.verify

When you sending the token in postman, select Bearer Token

Bearer Token

When you start creating your frontend, the codes should be equivalent to the following fetch request

fetch('/api/path', { method: 'GET', headers: { "Authorization": `Bearer ${token}`}}).(res => res.json())

May change method to your desire method (e.g get or post), and the token will be the the jwt token

Post a Comment for "Get Information From User In Database"