// [1] IMPORT SECT. // - const dotEnv = require("dotenv").config({ path: "./config/.env" }); const jwt = require("jsonwebtoken"); const { User } = require("../config/database"); // [=>] EXPORT middleware module.exports = async function (req, res, next) { try { token = req.headers.authorization.split(" ")[1]; // verify token const decodedToken = jwt.verify(token, process.env.SECRET_TOKEN); const user = await User.findByPk(decodedToken.userId); req.user = user; next(); } catch (error) { res.status(401).json({ message: error.message }); } };
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter