Home > Net >  Req.Params.Token returns undefined
Req.Params.Token returns undefined

Time:04-06

In my code, I have my registration page send out a verification email once the form is filled out and submitted. They receive the link to their email to verify it, and the link should trigger a request to change the account from inactive to active. I’m not sure if I did something wrong here but the code is as follows.

Where the user ends up after clicking the link.

app.get('/verify-email', (req, res, next) => {
console.log('req.params.token: '   req.params.token);
changeAccountStatus(req.params.token);
res.redirect('/login');});

The function that the token is passed through.

function changeAccountStatus(verifyingToken){
connection.query('SELECT * FROM ACCOUNTS WHERE VerificationToken= ?', [verifyingToken], function(error, results, fields) {
    if (error) {
        console.log("Error");
        console.log(error);
        return;
    }
    else {
        console.log('results from WHERE VerificationToken '   results[0]);
        connection.query('UPDATE ACCOUNTS SET STATUS = ? WHERE VerificationToken = ?', ['Active', verifyingToken], function (error, results, fields) {
            if (error) {
                console.log("Error");
                console.log(error);
            }
            else {
                console.log('results from UPDATE ACCOUNTS '   results[0]);
            }            
        });        
    }
});};

In the app.get(‘/verify-email’) method, the token returns undefined. I’m not sure why, I know that I get a token because an email is send and the token is pushed to the db.

CodePudding user response:

Are you sure that your token is in params. As normally, token suppose to be in headers. try req.headers.authorization instead of req.params.token to check if it is there.

CodePudding user response:

I needed to change req.params.token to req.query.token. The next thing I also had to do was move everything is side the function to the get method.

  • Related