Express server not setting cookies

Viewed 55

I have been trying to figure out what is wrong with this code for setting cookies in the server after login authorization from the client. Currently, it will print to the console en empty object that represents cookies. Once the backend server has verified the login it sets the cookies like this:

// UserController.js (server-side)

    res.cookie('token', token )
       .cookie('username', username)
       .status(201)
       .send(username)

This then returns back to this function which redirects it a new page

/// Login.js (client-side)

  const login = async () => {
      var token = params.get("token")

      const config = {
        method: 'POST', 
        url: url + "/users/fbLogin", 
        headers: {
          "Content-Type": "application/json",
          "Access-Control-Allow-Origin": "http://localhost:3000"
        }, 
        data: {
          fb_id: fb_id, 
          token: token, 
        }
      }

      var res = await axios(config)
      if(res.status === 201) {
        window.location.assign(`${client_url}/home?user=${res.data}`);
      }
  } 

When the page is redirected it sends a get request back to the server like so:

// UserHome.js

function validateUser(username){

    // axios.defaults.withCredentials = true
    const config = {
        method: 'GET', 
        url: url + '/users/auth', 
        // withCredentials: 'true', 
        headers: {
            "Content-Type": "application/json",
            'Access-Control-Allow-Credentials': true,
            "Access-Control-Allow-Origin": "http://localhost:3000"
        }, 
        data: {
            username: username
        }, 
    }
    try {

        var res = axios(config)

    } catch(err) {
        console.log(err)
    }

}

this request is handled by this function:

// UsersController.js 

UsersController.auth = async function(req, res) {
    console.log(req.cookies)
}

which currently prints out this: [Object: null prototype] {}

Does anyone know what I am clearly doing wrong? I am testing it also in Chrome and Safari and having the same issues.

The express setup:


const express = require('express');
const bodyParser = require('body-parser');
const app = express(); 
const cors = require('cors');
const api =  require('./api/index.js'); 
const passport = require('passport')
const cookieParser = require("cookie-parser");
var cookieSession = require('cookie-session')


// setup the connection
const port = process.env.PORT || 5000;
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// use it before all route definitions
app.use(cors({origin: 'http://localhost:3000'}));

app.use(passport.initialize());
app.use(passport.session());

app.use(cookieSession({
    auth: 'session', 
    keys: 'secret', 
    maxAge: 900000,
    httpOnly: false, 
    sameSite: false, 
    overwrite: true
}))

app.use(cookieParser());
app.use('/', api); 

app.listen(port, () => console.log(`Listening on port ${port}`));


0 Answers
Related