bug in getCurrentUser() Method (PHP)

can’t call function doLogoff()

public function doLogin(Request $request)
{
try
{
Backendless::$UserService->login( $request->username, $request->password);
$user = Backendless::$UserService->getCurrentUser();
return redirect(’/’);
}
catch(BackendlessException $e)
{
return redirect(’/’)->withErrors($e->getMessage());
}
}
public function doLogoff(Request $request)
{
$user = Backendless::$UserService->getCurrentUser();
Backendless::$UserService->logout($user);
return redirect(’/’);
}
got this error : Backendless API return error: Unable to logout user due to error: user must be logged in… Error code:3023

i already call the doLogin() first and get the current user data, but when i call the doLogoff function I got the error, i already try to do dd($user); in function doLogoff() and got “null” value.

conclusion : i think the API for Backendless::$UserService->getCurrentUser(); returns not null when inside the same function that calls Backendless::$UserService->login(user,pass); ,but when i try to call it in different function (that is not calling Backendless::$UserService->login(user,pass); API), it will give a null result. Is it a bug?? and what is the purpose of the Logout API if i cannot use the getCurrentUser method?

Hi Wendy,

This happens since the information about the user is not persisted anywhere. The following may help you to preserve the intermediate state:

//---------------------- login page logic-----------------------------------------

Backendless::$UserService->login( useremail, password ); 
$_SESSION['user'] = Backendless::$UserService->getCurrentUser(); 

//or short version

$_SESSION['user'] = Backendless::$UserService->login( useremail, password ); 

//---------------------- all pages ---------------------------------------------
//as in any other web application we need init user using session or cookies

if( !empty( $_SESSION['user'] ) )
 {
 Backendless::$UserService->setCurrentUser( $_SESSION['user'] ); 
}

//---------------------- logout page logic -----------------------------------------

Backendless::$UserService->logout( Backendless::$UserService->getCurrentUser() );

Regards,
Mark

ok thanks for the explanation

Regards,

Wendy

Hey Mark, I tried that but I kept getting an exception that states that $_SESSION[‘user’] should be a BackendlessUser class instance at the setCurrentUser method.

Here is what I do, at the login page I put the resulting object of the login in $_SESSION[‘user’]. At other pages, when I retrieve the $_SESSION[‘user’], the resulting object is no longer a BackendlessUser type, instead it’s a class __PHP_Incomplete_Class.

And I guess that’s causing the setCurrentUser on other pages to fail.

Okay I figured I had to add this line before session_start() on the other pages

include 'path to BackendlessUser class php";

Apparently when moving an object of type Backendless class through session messes up its type when receiving it in other pages. It has to do with the way PHP serialises and deseriallizes the object. However, is this the way Backendless does this? That is, including paths to the sdk classes?