0

I have my react app running on localhost:3000 & my node server api on localhost:9000

app.use(cors());

app.use(session({
    secret: 'secret'
    resave: false,
    saveUninitialized: false,
    cookie: {
        domain: 'localhost',
        secure: false
    },
    store: new MongoStore({
        mongooseConnection: db,
        autoRemoveInterval: 1,
        ttl: 259200 // 3 days
    })
}));

When a user authenticates, this works only when I serve my application from localhost:9000 but not from localhost:3000. I know the rule is that browsers don't set cookies from different domains. But is there a way achieve what I'm attempting without using local or session storage in the browser?

Garuuk
  • 1,629
  • 3
  • 26
  • 47
  • Can you describe exactly what does and doesn't work. I don't follow what exactly you're asking. What specific request does not work properly? Please show that request, both client and server code. – jfriend00 Feb 20 '21 at 22:33
  • I think it's a passport issue & not an express issue: https://stackoverflow.com/questions/37652378/local-passport-authorization-on-different-ports I'm trying to do this here. – Garuuk Feb 21 '21 at 02:18

1 Answers1

0

This can be solved within create-react-app

https://create-react-app.dev/docs/proxying-api-requests-in-development/#configuring-the-proxy-manually

I ended up following that and it works perfectly. I also enabled cors on my dev server for development.

Garuuk
  • 1,629
  • 3
  • 26
  • 47