parse-community / parse-php-sdk

The PHP SDK for Parse Platform
https://parseplatform.org/
Other
812 stars 346 forks source link

Question: an example to authenticate with user_id and a token? #480

Closed xenogenesi closed 2 years ago

xenogenesi commented 2 years ago

I am working on a Laravel 8 project with Parse as backend, I created a service to lazy initialize ParseClient with the configuration, I created a ParseUserProvider and ParseAuthenticatable for login and it works (Laravel docs: Adding Custom User Providers).

I'd like, but It is not clear to me how, to enable the "remember me" feature with the API provided by the SDK.

Laravel provides a long-lasting cookie if the "remember me" checkbox is true at login. When a user reloads the page if the cookie is set, retrieveByToken($identifier, $token) is called in UserProvider.

I tried to add a remember_token field to the _Session table, I use it to retrieve the sessionToken and then use it with become(), but it doesn't seem to work, in particular, unlike the login, the user is not copied to $_SESSION (and getCurrentUser() keeps failing).

What am I missing? Is there any other way?

retrieveByToken implementation ```php /** * Retrieve a user by their unique identifier and "remember me" token. * * @param mixed $identifier * @param string $token * @return \Illuminate\Contracts\Auth\Authenticatable|null */ public function retrieveByToken($identifier, $token) { Log::debug('retrieveByToken', [$identifier, $token]); $parseService = App::make(ParseService::class); try { $sessionQuery = new ParseQuery("_Session"); $sessionQuery->equalTo("remember_token", $token); $useMasterKey = true; $session = $this->castToParseSession($sessionQuery->first($useMasterKey)); if (is_a($session, ParseSession::class)) { Log::debug('retrieveByToken session is_a session', [$session]); $sessionToken = $session->get("sessionToken"); if (!is_null($sessionToken)) { $user = ParseUser::become($sessionToken); $authenticable = new ParseAuthenticatable([ 'id' => $user->getObjectId(), 'username' => $user->getUsername(), 'email' => $user->getEmail(), 'remember_token' => $token, ]); return $authenticable; } else { Log::debug('retrieveByToken session is null'); } } else { Log::debug('retrieveByToken session', [$session]); } } catch (ParseException $e) { Log::error("Failed to set remember me token in session", [$e->getCode(), $e->getMessage()]); } return null; } ```
mtrezza commented 2 years ago

I'm closing this as it does not seem to be a Parse PHP SDK issue.

Feel free to comment if you have any questions and we can re-open this issue.