I have a project builded in MEVN stack that works properly in local, I deployed my backend in Heroku successfully and I tested it with Postman and everything works very well, but when I tried to login with my client(local) i have a "GET https://invoicing-wmb.herokuapp.com/api/user 401 (Unauthorized)". any idea what could be wrong ?...
In addition I've been testin with console.log("") and the code works until before this line in User:
const claims = jwt.verify(cookie, 'secret')
Note: The only thing that is not working is the user auth everything else is working good.
This is the mi index.js in the backend.
const express = require('express')
const mongoose = require('mongoose')
const cors = require('cors')
const cookieParser = require('cookie-parser')
mongoose.set('useCreateIndex', true)
mongoose.connect('mongodb+srv://markselin:PASSWORD123@cluster0.uaau3.mongodb.net/myFirstDatabase?retryWrites=true&w=majority', {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true
}, () => {
console.log('connected to the database')
})
const routes = require('./routes/routes')
app = express()
app.use(cookieParser())
app.use(cors({
credentials: true,
origin: ['https://invoicing-wmb.herokuapp.com', 'http://localhost:8080'] //CLOUD
}))
app.use(express.json())
app.use('/api', routes)
//testing main /
app.get('/', function(peticion, respuesta){
respuesta.send('WECOME TO OUR API [WBM]')
})
app.get('/api', function(peticion, respuesta){
respuesta.send('INVOICING API [WMB]')
})
app.listen(process.env.PORT || 8000)
This is my User in router.js
router.get('/user', async (req, res) => {
try {
const cookie = req.cookies['jwt']
const claims = jwt.verify(cookie, 'secret')
if (!claims) {
res.send({message:'NO AUTENTICADO'})
}
const user = await User.findOne({_id: claims._id})
const {pass, ...data} = await user.toJSON()
console.log('AUTENTICADO'+data)
res.json(data)
} catch (e) {
return res.status(401).send({
message: 'unauthenticated'
})
}
})
Login method in client
methods: {
async handleSubmit() {
try {
const respons = await axios.post("login", {
email: this.email,
pass: this.pass,
});
localStorage.setItem("token", respons.data.token);
const response = await axios.get("user");
this.$store.dispatch("user", response.data);
this.$router.push({name:'Inicio'})
} catch (e) {
console.log(e)
}
},
and axios.js
import axios from 'axios'
axios.defaults.baseURL = 'https://invoicing-wmb.herokuapp.com/api/' //CLOUD
axios.defaults.headers.common['Authorization'] = 'Bearer' + localStorage.getItem('token')
axios.defaults.withCredentials = true;
and finally this is the logs from Heroku when I tried to Login
