gope153
gope153

Reputation: 85

Cross Domain Authentication with node.js and express-session

I'm using Passport.js for auth. Session Express-Session and Mongo-Connect to store it. I've got a problem. It stopped working. All the time everything was fine. No it's not. This is the code:

app.use(session({
    secret: 'secret',
    resave: true,
    // unset: 'destroy',
    domain: '.domain.com',
    saveUninitialized: false,
    cookie:  {
        // path: '/',
        domain: '.domain.com',
        maxAge: 24 * 6 * 60 * 10000
    },
    store: new MongoStore({url: config.db, autoReconnect:true})
}))

The auth process works fine, but it's not working anymore for cross domains.

Anyone got an idea? Checked for updates but didn't find anything.

Upvotes: 0

Views: 6341

Answers (1)

Amulya Kashyap
Amulya Kashyap

Reputation: 2373

Just handle CORS effect like this and add following code above your code

app.use(function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, authorization");
    res.header("Access-Control-Allow-Methods", "GET,POST,DELETE,PUT,OPTIONS");
    next();
});

Now, after this middleware you should put your code like this:

app.use(session({
    secret: 'secret',
    resave: true,
    // unset: 'destroy',
    domain: '.domain.com',
    saveUninitialized: false,
    cookie:  {
        // path: '/',
        domain: '.domain.com',
        maxAge: 24 * 6 * 60 * 10000
    },
    store: new MongoStore({url: config.db, autoReconnect:true})
})) 

Upvotes: 1

Related Questions