Hello-World-Software-Studios / calculator

Carpenter's tool for wall layout
0 stars 1 forks source link

Fix bug: Remove Bearer from Authorize header #61

Closed ecumene closed 3 years ago

ecumene commented 3 years ago
module.exports = async (req, res, next) => {
  try {
    const jwtToken = await req.header("Authorization");
    if (!jwtToken) {
      res.status(403).json({ message: "No access token" });
    }

    const payload = await jwt.verify(jwtToken.replace("Bearer ", ""), process.env.jwtSecret);
    res.json({ "id": payload.id });
  } catch (err) {
    res.status(500).json({ message: err.message });
  }
  next();
};

https://stackoverflow.com/questions/43915379/i-need-to-replace-bearer-from-the-header-to-verify-the-token

JPM709 commented 3 years ago

Used replace function as demonstrated above. Endpoints that use 'authorization.js' as middleware function now working correctly.

authorize now returns user id, which is then used to fetch relevant data from API.