Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

New parse.com php API - currentUser not giving back a user

I am able to login with user credentials with

try {
  $user = ParseUser::logIn("myname", "mypass");
  // Do stuff after successful login.
} catch (ParseException $error) {
  // The login failed. Check error to see why.
}

but if I try to get the currentUser afterwards with

$currentUser = ParseUser::getCurrentUser();
if ($currentUser) {
    // do stuff with the user
} else {
    // show the signup or login page
}

$currentUser is not set.

I suspect this more to be a php "issue" that I don't know. I am greatful for any hint for keeping currentUser retained in my code as long I do not log out.

like image 511
Peter Avatar asked Aug 17 '14 07:08

Peter


1 Answers

In order for the getCurrentUser() method to work, you must define the type of session storage to use. You can use the ParseSessionStorage class to achieve this:

use Parse\ParseClient;
use Parse\ParseUser;
use Parse\ParseSessionStorage;

session_start();

// Init parse: app_id, rest_key, master_key
ParseClient::initialize('xxx', 'yyy', 'zzz');

// set session storage
ParseClient::setStorage( new ParseSessionStorage() );

try {
  $user = ParseUser::logIn("myname", "mypass");
  // Do stuff after successful login.
} catch (ParseException $error) {
  // The login failed. Check error to see why.
}

$currentUser = ParseUser::getCurrentUser();

print_r( $currentUser );
like image 158
Niraj Shah Avatar answered Sep 28 '22 10:09

Niraj Shah