Node.js, Vue.js and Passport.js. .isAuthenticated() always returns false? Axios headers possibly?
Your issue is because your frontend and backend are on different domains.
Cookies, which passport.session()
/express.session()
use to maintain a user session, are scoped to a specific domain.
When you call axios.get()
on the protected resource, axios will not send or receive cookies because localhost:8080
is a different domain to localhost:8081
.
Try axios.get('/path/to/your/resource', { withCredentials: true })
and axios.post('/login', { username, password }, { withCredentials: true })
This would have been present even without Vue so long as you're using AJAX to make these calls.
requests.js
let axiosConfig = {
withCredentials: true,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': 'http://localhost:3000/',
'Access-Control-Allow-Methods': 'GET,PUT,POST,DELETE'
}
}
server.js
var express = require('express');
var app = express();
var cors = require('cors');
app.use(cors({credentials: true, origin: 'http://localhost:8080'}))
app.use(cors({credentials: true, origin: 'http://localhost:8080'}))