Hello, just as an idea, use browser local storage to temporarily store items in your cart, or https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API it's pretty reliable
Apple login with Socialite - clears my cart session
Hi everybody!
I have an ecommerce project where I've added social login with Socialite package. For apple login i have a problem that after returning from apple login form, it clears my cart session, so the user loses the products in cart.
Because apple requires a POST callback, I've excluded the URL in VerifyToken.php. I think this is the problem because it invalidates my session after coming back to my website.
Any suggestion how i can make this work?
Thank you in advance.
Warning: This workaround does no longer work due to changes by Apple.
See: https://github.com/SocialiteProviders/Providers/issues/1181#issuecomment-2243577153
I've created two classes to fix this:
<?php
namespace MN\SocialLogin\Apple\Socialite;
use SocialiteProviders\Apple\AppleExtendSocialite;
use SocialiteProviders\Manager\SocialiteWasCalled;
class SocialiteExtensionApple extends AppleExtendSocialite
{
public function handle(SocialiteWasCalled $socialiteWasCalled): void
{
$socialiteWasCalled->extendSocialite('apple', SocialiteProviderApple::class);
}
}
<?php
namespace MN\SocialLogin\Apple\Socialite;
use SocialiteProviders\Apple\Provider;
use Throwable;
/**
* Custom Apple Socialite Provider that hits the callback with GET requests instead of POST requests
*
* The original implementation uses `response_mode=form_post` in order to support scopes.
* Supported scopes are: `name` and `email`
* Apple only returns the requested scopes the very first time a user authorizes the app.
* Laravel seems to identify Apples `form_post` requests as new PHP sessions.
* This causes all sorts of issues and introduces a lot of complexity.
*
* We're dropping support for scopes here in order to be able to use `response_mode=query`.
* We still have access to the users email address every time the user authorizes the app.
* However, we never have access to the users name. This is a tradeoff that we can accept.
*
* In practice this means that new users that hide their emails via iCloud Privat-Relay will
* get weird usernames because their email addresses look something like this:
*
* [email protected]
*
* The default `SocialLoginStrategy::username` implementation would return `5zhkkwxrtt` in this case.
*
* @see https://laracasts.com/discuss/channels/laravel/apple-login-with-socialite-clears-my-cart-session?page=1&replyId=930156
* @see https://developer.apple.com/documentation/sign_in_with_apple/request_an_authorization_to_the_sign_in_with_apple_server#query-parameters
*/
class SocialiteProviderApple extends Provider
{
/**
* {@inheritdoc}
*/
protected $scopes = [];
/**
* {@inheritdoc}
*
* @throws Throwable
*/
protected function getCodeFields($state = null): array
{
throw_if(
count($this->scopes),
'Scopes are not supported when using the `apple` driver. '
.'Scopes would require us to use `response_mode=form_post` which regenerates the PHP session.',
);
$fields = parent::getCodeFields($state);
unset($fields['scope']);
$fields['response_mode'] = 'query';
return $fields;
}
}
Then instead of this:
protected $listen = [
\SocialiteProviders\Manager\SocialiteWasCalled::class => [
// ... other providers
\SocialiteProviders\Apple\AppleExtendSocialite::class.'@handle',
],
];
I registered my own handler:
protected $listen = [
\SocialiteProviders\Manager\SocialiteWasCalled::class => [
// ... other providers
\MN\SocialLogin\Apple\Socialite\SocialiteExtensionApple::class.'@handle',
],
];
Now you can also use a simple GET route for the callback. Also no need to mess with CSRF token protection etc.
Make sure to read the doc block of SocialiteProviderApple it explains what's going on. There is a tradeoff!
Please or to participate in this conversation.