TypeError: Cannot destructure property 'email' of 'decodedtoken' as it is undefined when trying to decode my jwt token

Viewed 24

I am currently creating a site in javascript. When a user tries to create an account on this site, an email is sent to him containing a button whose click sends a request containing a token to the server of my application. Here is the code for creating my token:

let {  email, password, firstName, lastName, gender, platform, coords } = req.body;
 ...
const activate_token = createJwtToken({ email, password, firstName, lastName, gender, platform, coords  });

My createJwtToken function:

// generate json web token from payload of userId 
exports.createJwtToken = (payload) => {
  const token = jwt.sign(payload, JWT_SECRET, { expiresIn: "12h" });
  return token;
};

The content of my email:

<a href="${port_server}/api/auth/activate/${activate_token}"><button>activate your account to use whelps</button></a>

The account activation code:

exports.activateAccount = async (req, res, next) => {
    // extract json web token
    const {token} = req.body;
    try {

        jwt.verify(token, JWT_SECRET, exports.function = async (req, res, next, decodedtoken) =>{

            const { email, password, firstName, lastName, gender, platform, coords } = decodedtoken;
      .. .
 })
    } catch (error) {
        next(error);
    }
};

When I test my application, the email containing the button is sent to the user but the server generates an error:

const { email, password, firstName, lastName, gender, platform, coords } = decodedtoken;
                    ^

TypeError: Cannot destructure property 'email' of 'decodedtoken' as it is undefined.

I do not understand why this error is generated yet the value of the email is indeed contained by the token. I even tried to find the solution on this stackoverflow question but nothing. Thanks!

0 Answers
Related