phpfui / ConstantContact

MIT License
16 stars 7 forks source link

How to use setSessionCallback? #13

Closed pixelone closed 1 year ago

pixelone commented 1 year ago

Thanks for this library, your time is greatly appreciated!

Any example on how to use the setSessionCallback? I was testing this with Laravel real quick, but I am unable to get $client->accessToken or $client->refreshToken to output. If I switch to using PHP's start_session() it works, but the application I want to implement into uses its own session handler.

I assume I am doing the callback incorrect or missing something?

$client = new Client($apiKey, $secret, $redirectURI);

$client->setSessionCallback(function ($key, $value) use ($request) {
    $request->session()->put($key, $value);
    return $request->session()->get($key) ?? '';
});

$client->acquireAccessToken($_GET);

echo $client->accessToken . '<br>';
echo $client->refreshToken . '<br>';

dd($request->session()->all());`
phpfui commented 1 year ago

Looks like you are not following the contact the callback expects. See http://phpfui.com/?n=PHPFUI%5CConstantContact&c=Client

Basically you need to delete the session variable if passed in a null value, otherwise store it.

Let me know if that doesn't work.

On Thu, Feb 2, 2023, 5:52 PM pixelone @.***> wrote:

Thanks for this library, your time is greatly appreciated!

Any example on how to use the setSessionCallback? I was testing this with Laravel real quick, but I am unable to get $client->accessToken or $client->refreshToken to output. If I switch to using PHP's start_session() it works, but the application I want to implement into uses its own session handler.

I assume I am doing the callback incorrect or missing something?

$client = new Client($apiKey, $secret, $redirectURI);

$client->setSessionCallback(function ($key, $value) use ($request) { $request->session()->put($key, $value); return $request->session()->get($key) ?? ''; });

$client->acquireAccessToken($_GET);

echo $client->accessToken . '
'; echo $client->refreshToken . '
';

dd($request->session()->all());`

— Reply to this email directly, view it on GitHub https://github.com/phpfui/ConstantContact/issues/13, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABYW6S6LM4I2MXL3B6Q72Q3WVRJETANCNFSM6AAAAAAUPVZPXA . You are receiving this because you are subscribed to this thread.Message ID: @.***>

pixelone commented 1 year ago

Thanks I sorted by looking at your method and with the following.

$client->setSessionCallback(function ($key, $value) use ($request) {
    if (null === $value) {
        $value = $request->session()->get($key) ?? '';
        $request->session()->forget($key);

        return $value;
    }

    $request->session()->put($key, $value);

    return $request->session()->get($key);
});`

Another quick question: When you call $client->refreshToken(); should the refreshToken be updated right away? I call $client->refreshToken(), but not seeing any changes to $client->refreshToken output.

phpfui commented 1 year ago

Yes, your new implementation looks correct now.

And yes, it should update the refresh token after the call.

On Thu, Feb 2, 2023, 8:03 PM pixelone @.***> wrote:

Thanks I sorted by looking at your method and with the following.

$client->setSessionCallback(function ($key, $value) use ($request) { if (null === $value) { $value = $request->session()->get($key) ?? ''; $request->session()->forget($key);

    return $value;
}

$request->session()->put($key, $value);

return $request->session()->get($key);

});`

Another quick question: When you call $client->refreshToken(); should the refreshToken be updated right away? I call $client->refreshToken(), but not seeing any changes to $client->refreshToken output.

— Reply to this email directly, view it on GitHub https://github.com/phpfui/ConstantContact/issues/13#issuecomment-1414756200, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABYW6S3PJJ7CSRFCCYWACC3WVRYRXANCNFSM6AAAAAAUPVZPXA . You are receiving this because you commented.Message ID: @.***>

pixelone commented 1 year ago

Seems to be working. Thanks for the help!