Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cloud Code Parse.User.current() return null

When i use this function in Cloud Code Parse.User.current() return null.
I'm using parseExpressCookieSession for login.
Any advice?

var express = require('express');
var expressLayouts = require('cloud/express-layouts');
var parseExpressHttpsRedirect = require('parse-express-https-redirect');
var parseExpressCookieSession = require('parse-express-cookie-session');

// Required for initializing enter code hereExpress app in Cloud Code.
var app = express();

// Global app configuration section
app.set('views', 'cloud/views');
app.set('view engine', 'ejs'); // Switch to Jade by replacing ejs with jade here.
app.use(expressLayouts);          // Use the layout engine for express
app.set('layout', 'layout');
app.use(parseExpressHttpsRedirect());  // Require user to be on HTTPS.
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser('helloworld'));
app.use(parseExpressCookieSession({
  fetchUser: true,
  cookie: { maxAge: 3600000 * 24 }
}));    

Parse.Cloud.beforeSave('Menu', function(request, response) {
    var Business = Parse.Object.extend('Business');
    var query = new Parse.Query(Business);

    query.equalTo('profile', Parse.User.current().get('profile'));
    query.find({
        success: function(business) {
            console.log(business);
            response.success();
        },
        error: function(error) {
            response.error(error.message);
        }
    });
  });

  app.listen();

This the code that i use to login/logout

app.post('/login', function(req, res) {
  Parse.User.logIn(req.body.username, req.body.password).then(function(user) {
    // Login succeeded, redirect to homepage.
    // parseExpressCookieSession will automatically set cookie.
    res.redirect('/');
  },
  function(error) {
    // Login failed, redirect back to login form.
    res.redirect('/');
  });
});

// Logs out the user
app.post('/logout', function(req, res) {
  Parse.User.logOut();
  res.redirect('/');
});
like image 999
user3864472 Avatar asked Aug 29 '14 13:08

user3864472


1 Answers

It is an old question but answering for future reference.

Parse.User.current() works in Javascript SDK when used in clients ex. WebApp where users log in and the you can fetch the current user with that function.

To get the user calling a Cloud Code function or doing an operation on an object (beforeSave,afterSave,beforeDelete and so on) you use the request.user property it contains the user issuing the request to Parse.com.

More details about Parse.Cloud.FunctionRequest here: https://parse.com/docs/js/api/classes/Parse.Cloud.FunctionRequest.html

Example code:

Parse.Cloud.beforeSave('Menu', function(request, response) {
   var requestUser = request.user; 
   // instance of Parse.User object of the user calling .save() on an object of class "Menu"

   // code cut for brevity
});
like image 144
zagu Avatar answered Oct 12 '22 16:10

zagu