parse-php-sdk: authenticate with user_id and a token?

  laravel, parse-platform, php

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?

    /**
     * Retrieve a user by their unique identifier and "remember me" token.
     *
     * @param  mixed  $identifier
     * @param  string  $token
     * @return IlluminateContractsAuthAuthenticatable|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;
    }

Source: Laravel

Leave a Reply