Matt The Ninja
Matt The Ninja

Reputation: 2731

User Sessions Parse.com Cloud Code Hosting

My login form currently posts to itself on / The post request is then picked up as follows...

app.post('/', userController.doLogin);

Then the controller is a follows..

exports.doLogin= function(req, res) {
    Parse.User.logIn(req.body.username, req.body.password, {
      success: function(user) {
        console.log('login success');
        res.render('loginSuccess');
      },
      error: function(user, error) {
        console.log('login failed');
        res.render('loginFailed');
      }
    });
}

This works correctly for a correct / incorrect login.

However, once logged in, the session is not stored no cookies are created / local storage etc..

Therefore when I test for login on one of my other routes it always displays as no-session, i am checking with the following code..

if(Parse.User.current()){
        console.log('logged in and redirected');
        res.redirect('/member/home');
    }else{
        console.log('not logged in, redirected to home/login page');
        res.redirect('/');
    }

Which always goes too home / again.

I read here https://parse.com/docs/hosting_guide#webapp-users that...

You just need to call Parse.User.logIn() in Cloud Code, and this middleware will automatically manage the user session for you.

Which would suggest it does the session for me?

Any help would be super useful! many thanks in advance!

Upvotes: 2

Views: 363

Answers (1)

Matt The Ninja
Matt The Ninja

Reputation: 2731

Ok so after a lot of digging I have worked it out. First you need to add the module by doing the following..

var parseExpressCookieSession = require('parse-express-cookie-session');

Second you need to setup your own variables like this

app.use(express.cookieParser('YOUR_SIGNING_SECRET'));

app.use(parseExpressCookieSession({ cookie: { maxAge: 3600000 } }));

Third, you must do send the login/session all over HTTPS.

Boom, working - easy peasy when you know how.

Upvotes: 1

Related Questions