Since the built in SessionGuard
does not have a way to change this, you will need to create your own Guard
class to override the method, and tell Auth
to use your Guard
class. This information is also explained in my answer here, which explains how to customize the TokenGuard
.
First, start by creating a new Guard
class that extends the base SessionGuard
class. In your new Guard
class, you will override the getRecallerName()
method to return the name you want. In this example, it is created at app/Services/Auth/MySessionGuard.php
:
namespace App\Services\Auth;
use Illuminate\Auth\SessionGuard;
class MySessionGuard extends SessionGuard
{
/**
* Get the name of the cookie used to store the "recaller".
*
* @return string
*/
public function getRecallerName()
{
return 'myprefix_'.$this->name.'_'.sha1(static::class);
}
}
Once you've created your class, you need to let Auth
know about it. You can do this in the boot()
method on your AuthServiceProvider
service provider:
public function boot(GateContract $gate)
{
$this->registerPolicies($gate);
Auth::extend('mysession', function($app, $name, array $config) {
$provider = $this->createUserProvider($config['provider']);
$guard = new \App\Services\Auth\MySessionGuard($name, $provider, $app['session.store']);
// When using the remember me functionality of the authentication services we
// will need to be set the encryption instance of the guard, which allows
// secure, encrypted cookie values to get generated for those cookies.
if (method_exists($guard, 'setCookieJar')) {
$guard->setCookieJar($app['cookie']);
}
if (method_exists($guard, 'setDispatcher')) {
$guard->setDispatcher($app['events']);
}
if (method_exists($guard, 'setRequest')) {
$guard->setRequest($app->refresh('request', $guard, 'setRequest'));
}
return $guard;
});
}
And finally, you need to tell Auth
to use your new mysession
guard. This is done in the config/auth.php
config file.
'guards' => [
'web' => [
'driver' => 'mysession',
'provider' => 'users',
],
],