Fran Exposito
Fran Exposito

Reputation: 41

Manage sessions in Cloud Code and current.user on Parse.com

I'm going to try to explain my problem the clearest possible way. I'm using the jssdk 1.6.7. In order to manage the sessions I use parse-express-cookie-session:

var parseExpressHttpsRedirect = require('parse-express-https-redirect');
var parseExpressCookieSession = require('parse-express-cookie-session');
app.use(parseExpressHttpsRedirect());
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser('mykey'));
app.use(parseExpressCookieSession({
  fetchUser: true,
  cookie: { maxAge: 3600000 }
}));
app.post('/login', function(req, res) {
  Parse.User.logIn(req.body.username, req.body.password).then(function() {
    res.send(true);
  }, function(error) {
    res.send(error);
  });
});

When I do login from the website, the cookie gets created without any problem. When an user access to an url such as /prueba I retrieve the user with:

app.get('/prueba', function(req, res) {
  res.send(Parse.User.current());
});

The problem is when on Javascript I call the cloud method on the client-side, Parse.User.current() and request.user are always null and I can't get the currentUser. Any ideas?

Edit for more info:

When I do for example:

Parse.Cloud.run("getNumbersWords", {id: country, userc: cUser}, {
  success: function(count) {
    numberWords = count;
    changePalabra();
  },
  error: function(error) {
    console.log(error);
   }
});

My getNumbersWords is:

Parse.Cloud.define('getNumbersWords', function (request, response) {
  console.error(request);
  var query = new Parse.Query("Pais");
  query.get(request.params.id).then( function(pais) {
    var queryP = new Parse.Query('Palabras');
    queryP.equalTo('idioma', pais.get('idioma').toLowerCase());
    return queryP.count();
  }).then( function(count) {
    response.success(count);
  }, function(error) {
    response.error({'resp': error.code, 'message': error.message});
  });
});

Parse.User.current(), request.user and request.params.sessionToken are null.

Upvotes: 2

Views: 1119

Answers (1)

Fran Exposito
Fran Exposito

Reputation: 41

Ok so after a lot of digging I have worked it out. First you need to do something like this in your client-side:

Parse.User.logIn(usern, pass, {
  success: function(user) {
    var sess = user.get("sessionToken");
    $.ajax({
      type: 'POST',
      dataType: "json",
      data: {sessionToken: sess},
      url: "/login",
      success: function(resp) {
        console.log(resp)
      },
      error: function(error) {
        console.log(error);
      }
    });
  },
  error: function(error) {
    console.log(error);
  }
});

In your server-side, your /login post must be this:

app.post('/login', function(req, res) {
  var sessionToken = req.body.sessionToken;

  Parse.User.become(sessionToken).then(function (user) {
    res.send(true);
  }, function (error) {
    console.log(error);
    res.send(false);
  });
});

Et voilà. The "become" method puts a cookie in the browser.

Upvotes: 2

Related Questions