UNPKG

mern-backend-quickstart

Version:

CLI tool to quickly generate a MERN backend boilerplate with optional auth middleware.

21 lines (17 loc) 593 B
import jwt from 'jsonwebtoken'; import Sample from '../models/sampleModel.js'; export const protect = async (req, res, next) => { let token = req.headers.authorization?.split(' ')[1]; if (!token) { res.status(401); throw new Error('Not authorized, no token'); } try { const decoded = jwt.verify(token, process.env.JWT_SECRET); req.user = await Sample.findById(decoded.id).select('-password'); next(); } catch (error) { res.status(401); throw new Error('Not authorized, token failed'); } };