I'm using jwt for confirm an email, I send an email to user with the URL with the token, it is an example of the URL that receive the user:
http://localhost:3000/firstlogin?acces_token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6InJleWVzZUBncnVwb2ludmVyby5jb20iLCJpYXQiOjE1OTY2Njk4OTcsImV4cCI6MTU5Njc1NjI5N30.dt-qr8h40QfPMOq-DQDfQVHatrAqOubKdlUbV4-nsTw
I'm using an query how you can see.
and the confirm code is the next:
const expressJwt = require('express-jwt')
const _ =require('lodash')
const { OAuth2Client } = require('google-auth-library')
const fetch = require('node-fetch')
const {validationResult} = require('express-validator')
const jwt = require('jsonwebtoken')
//Esto es para obtener el error de la base de datos, se puede personalizar para hacerlo amigable.
const { errorHandler} =require('../helpers/dbErrorHandling')
//el siguiente const se usará para enviar correos
const sgMail = require('@sendgrid/mail')
sgMail.setApiKey(process.env.MAIL_KEY)
const pool = require('../database/connection')
class FirstloginController {
async getToken(req, res, next) {
const token = (req.params.token);
const { access_token } = req.query;
try {
const decoded = jwt.verify(access_token, process.env.JWT_ACCOUNT_ACTIVATION)
res.json({ message: "El acces token es valido", email: decoded})
} catch (err) {
next(err);
}
}
}
const firstloginController = new FirstloginController();
module.exports = firstloginController;
The request is a get type, it brings me the next(on postman):
<pre>JsonWebTokenError: jwt must be provided<br> at Object.module.exports [as verify] (C:\xampp\htdocs\seth\api\node_modules\jsonwebtoken\verify.js:53:17)<br> at getToken (C:\xampp\htdocs\seth\api\src\controllers\Firstlogin.controller.js:24:33)<br> at Layer.handle [as handle_request] (C:\xampp\htdocs\seth\api\node_modules\express\lib\router\layer.js:95:5)<br> at next (C:\xampp\htdocs\seth\api\node_modules\express\lib\router\route.js:137:13)<br> at Route.dispatch (C:\xampp\htdocs\seth\api\node_modules\express\lib\router\route.js:112:3)<br> at Layer.handle [as handle_request] (C:\xampp\htdocs\seth\api\node_modules\express\lib\router\layer.js:95:5)<br> at C:\xampp\htdocs\seth\api\node_modules\express\lib\router\index.js:281:22<br> at Function.process_params (C:\xampp\htdocs\seth\api\node_modules\express\lib\router\index.js:335:12)<br> at next (C:\xampp\htdocs\seth\api\node_modules\express\lib\router\index.js:275:10)<br> at Function.handle (C:\xampp\htdocs\seth\api\node_modules\express\lib\router\index.js:174:3)
I have no idea whats significate that; the terminal shows me the next:
GET /firstlogin?acces_token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6InJleWVzZUBncnVwb2ludmVyby5jb20iLCJpYXQiOjE1OTY2Njk4OTcsImV4cCI6MTU5Njc1NjI5N30.dt-qr8h40QfPMOq-DQDfQVHatrAqOubKdlUbV4-nsTw 500 5.011 ms - 1275
JsonWebTokenError: jwt must be provided
at Object.module.exports [as verify] (C:\xampp\htdocs\seth\api\node_modules\jsonwebtoken\verify.js:53:17)
at getToken (C:\xampp\htdocs\seth\api\src\controllers\Firstlogin.controller.js:24:33)
at Layer.handle [as handle_request] (C:\xampp\htdocs\seth\api\node_modules\express\lib\router\layer.js:95:5)
at next (C:\xampp\htdocs\seth\api\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (C:\xampp\htdocs\seth\api\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (C:\xampp\htdocs\seth\api\node_modules\express\lib\router\layer.js:95:5)
at C:\xampp\htdocs\seth\api\node_modules\express\lib\router\index.js:281:22
at Function.process_params (C:\xampp\htdocs\seth\api\node_modules\express\lib\router\index.js:335:12)
at next (C:\xampp\htdocs\seth\api\node_modules\express\lib\router\index.js:275:10)
at Function.handle (C:\xampp\htdocs\seth\api\node_modules\express\lib\router\index.js:174:3)
It is basically all that I know, This process is in a different document to the process of send mail
You have written the wrong acces_token spelling
Use
const { acces_token } = req.query;