Last active
June 21, 2021 09:19
-
-
Save jedimdan/e6c78a2665f788eda9a3e3f3b4e1f982 to your computer and use it in GitHub Desktop.
A Laravel middleware that will set cookies to SameSite=None and account for incompatible clients. Note that it relies on https://github.com/jenssegers/agent as a dependency
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Middleware; | |
use Closure; | |
use Jenssegers\Agent\Agent; | |
class SameSiteNone | |
{ | |
/** | |
* Sets SameSite=None while checking for incompatible browsers | |
* | |
* @param \Illuminate\Http\Request $request | |
* @param \Closure $next | |
* @return mixed | |
*/ | |
public function handle($request, Closure $next) | |
{ | |
// parse the User Agent | |
$agent = new Agent; | |
$browser = $agent->browser(); | |
$browserVersion = $agent->version($browser, Agent::VERSION_TYPE_FLOAT); | |
// check for incompatible browsers based on https://www.chromium.org/updates/same-site/incompatible-clients | |
$browserIsCompatible = true; | |
if ($browser == 'Safari' && $agent->match('Mac OS X 10_14_*|iP.+; CPU .*OS 12_')) { | |
$browserIsCompatible = false; | |
} elseif ($browser == 'Chrome' && $browserVersion > 50 && $browserVersion < 67) { | |
$browserIsCompatible = false; | |
} elseif ($browser == 'UCBrowser' && $browserVersion < 12.13) { | |
$browserIsCompatible = false; | |
} | |
// set SameSite none to supported browsers only | |
if ($browserIsCompatible) { | |
config(['session.secure' => true]); | |
config(['session.same_site' => 'none']); | |
} else { | |
config(['session.secure' => false]); | |
config(['session.same_site' => null]); | |
} | |
return $next($request); | |
} | |
} |
@muhammadasfar it's working in as far as I'm using it on my production. You might also want to check out this PR that we are working on gnikyt/laravel-shopify#375
@jedimdan Thanks
Yes, I know that you guys are working hard over it and I really appreciate it.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@jedimdan is this a working solution for ohmybrew shopify-laravel apps?