I'm following the following tutorial to create an authentication system using Node.js and JWT: link
Following the steps in the tutorial, I can verify that the user is correct and create JWT. However, the token is apparently not being stored anywhere. When accessing the route / test, nothing is returned. Here is the code:
const express = require('express');
const jwt = require('jsonwebtoken');
const router = express.Router();
const Usuario = require('../models/Usuario');
router.get('/teste', (req, res) => {
const token = req.body.token || req.query.token || req.headers['x-access-token'] || null;
return res.json(token);
});
router.post('/login', (req, res) => {
Usuario.findOne({ email: req.body.email, senha: req.body.senha }, (err, usuario) => {
if (err) return res.json({ error: err });
if (!usuario) return res.json({ error: 'Email e/ou senha incorretos!' });
jwt.sign(usuario, 'secret', { expiresIn: 3600 }, (err, token) => {
if (err) return res.json({ error: err });
return res.json({ message: 'Logado com sucesso!', token: token });
});
});
});
module.exports = router;