'Req.Params.Token returns undefined
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.
Solution 1:[1]
I needed to change req.params.token to req.query.token. Everything was resolved that this point, but make sure that your functions are setup correctly too.
Solution 2:[2]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | JonD |
Solution 2 | Kumar Saurabh |