Skip to content

Middleware for fetching the user information #18

@abhishek-p-s

Description

@abhishek-p-s

function getUserDataFromReq(req) { return new Promise((resolve, reject) => { jwt.verify(req.cookies.token, jwtSecret, {}, async (err, userData) => { if (err) throw err; resolve(userData); }); }); }

instead of calling this function again and again we can create a middleware and we can add that midleware on each request and there is no need to call this function again and again

Why middleware is better than calling the function repeatedly
❌ Current approach (function call everywhere)

You must call getUserDataFromReq(req) in every route

Easy to forget → security bug

Repeated boilerplate

Poor separation of concerns

✅ Middleware approach (best practice)

JWT verification runs before the controller

Controller receives already-authenticated user

Cleaner, safer, reusable

Scales well as your app grows (especially important for MERN / Nest-style APIs)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions