Skip to content
Discussion options

You must be logged in to vote

@4gels Great question! Here's a complete, working example of JWT authentication with mgzon middleware, including how to access the authenticated user in your route handlers.

🔐 Complete JWT Authentication Example

1. Install the required dependency

npm install jsonwebtoken

2. Create the authentication middleware

// middleware/auth.js
const jwt = require('jsonwebtoken');

const JWT_SECRET = process.env.JWT_SECRET || 'your-secret-key';

function authMiddleware(req, res, next) {
  // Get token from Authorization header
  const authHeader = req.headers.authorization;
  const token = authHeader?.split(' ')[1]; // Format: "Bearer <token>"

  if (!token) {
    // Return an error response — mgzon w…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by 4gels
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants