How to get user.sub in Node.js/Express backend?

I have a simple ToDo app using React frontend, Nodejs/Express API backend, and MySQL DB. My tasks DB has a userId column which pulls the user.sub value on the frontend before sending the task to the backend and creating the DB entry.

Next, I want users to be able to see only their own tasks as identified by the userId column - to do this, I want to pull the user.sub value of the current logged-in user and dynamically insert it into my backend SQL query.


Task.getAll = (userId, result) => {

    let query = `SELECT * FROM tasks WHERE userId LIKE '${userId}'`;

    sql.query(query, (err, res) => {
        if (err) {
            console.log("error: ", err);
            result(null, err);

        console.log("tasks: ", res);
        result(null, res);

If I hardcode an existing auth0 user ID (“auth0|123456789…”) into the statement above, it successfully filters tasks based on ID - so the logic seems sound. I just need to be able to pull the user.sub value, which I have not been able to figure out in the backend. I have been following this quickstart document -

  1. Determine the User Identity

The express-jwt middleware which is used to validate the JWT, also sets the req.user with the information contained in the JWT. If you want to use the sub claim to identify the user uniquely, you can simply use req.user.sub.

…This sounds like what I want to achieve, but I can’t figure out how to actually do this in practice. How exactly can I pull the user.sub value from the checkJwt middleware?

Bumping for visibility.