I've been working on my website for quite some time and everything was smooth sailing, until now.
Here's the issue: after a user logs in, a session cookie named "user" is created to store their email. Upon console logging the cookie right after initialization in the login post request, it displays the correct data. However, when I navigate to another route like the home route, the cookie mysteriously becomes undefined.
In the login post route, Firebase API is utilized for authentication:
// Login POST Route
router.post('/login', (req, res) => {
// Firebase authentication service
firebase_user.auth().signInWithEmailAndPassword(req.body.email, req.body.password).then(data => {
// Cookie Init
req.session.user = req.body.email;
console.log(req.session.user); // The cookie value appears as intended here
}).catch(err => {
res.send({"error": err.message});
});
});
Now, let's take a look at the home route:
router.get('/home', (req, res) => {
// Check if Session Cookie Exists
if (req.session.user) {
res.render('home.ejs');
} else {
res.redirect('/login');
console.log(req.session.user); // Despite initializing the cookie correctly, it shows 'undefined' here
}
});
Middleware setup:
app.use(bodyParser.json());
app.use(morgan('combined'));
app.set('view engine', 'ejs');
app.use(express.static('./public'))
app.set('views', path.join(__dirname, 'views'));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cookieParser());
app.use(session({secret:"Testasl",resave:false,saveUninitialized:true,cookie:{secure:!true}}));
// Routes
app.use(routes);
Let's examine how data is sent to the login method using Axios and Vue:
var urlLog = 'http://localhost:3000/login';
new Vue({
el: '#main',
data: {
email: '',
password: '',
showForm: true,
showPreloader: false,
errorMessage: '',
errorShow: false
},
methods: {
submitForm: function() {
// Form validation
if (this.email!='' && this.password!=''){
// Display Preloader
this.showForm=false;
this.showPreloader=true;
// Ajax Post Request
axios.post(urlLog, {
email: this.email,
password: this.password
}).then(res => {
if (res.error){
// Show form
this.showForm=true;
this.showPreloader=false;
// Display Error
this.errorShow = true;
this.errorMessage = res.error;
} else {
// do nothing
}
// Server Side error
}).catch(err => {
console.log(err);
});
} else {
this.errorShow = true;
this.errorMessage = 'All fields are necessary...';
}
}
}
});
Any insights on why this behavior is occurring?
**** EDITED ****
UPDATE: As I tinkered with cookies, specifically using the cookie-parser module to initialize one, an error popped up:
Error: Can't set headers after they are sent.
at validateHeader (_http_outgoing.js:491:11)
at ServerResponse.setHeader (_http_outgoing.js:498:3)
at ServerResponse.header (C:\Users\Thirsty-Robot\Desktop\Projects\Important\Robotics\Dashboard\node_modules\express\lib\response.js:767:10)
at ServerResponse.append (C:\Users\Thirsty-Robot\Desktop\Projects\Important\Robotics\Dashboard\node_modules\express\lib\response.js:728:15)
at ServerResponse.res.cookie (C:\Users\Thirsty-Robot\Desktop\Projects\Important\Robotics\Dashboard\node_modules\express\lib\response.js:853:8)
at router.get (C:\Users\Thirsty-Robot\Desktop\Projects\Important\Robotics\Dashboard\bin\routes.js:74:9)
at Layer.handle [as handle_request] (C:\Users\Thirsty-Robot\Desktop\Projects\Important\Robotics\Dashboard\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\Thirsty-Robot\Desktop\Projects\Important\Robotics\Dashboard\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (C:\Users\Thirsty-Robot\Desktop\Projects\Important\Robotics\Dashboard\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (C:\Users\Thirsty-Robot\Desktop\Projects\Important\Robotics\Dashboard\node_modules\express\lib\router\layer.js:95:5)
The cookie creation was done like this:
// Login GET Route
router.get('/login', (req, res) => {
res.render('log_in.ejs');
res.cookie('idk', 'idksj');
console.log(req.cookies);
});