-
Notifications
You must be signed in to change notification settings - Fork 11.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[9.x] Add custom segments on "remember me" for session rebuild #42316
Conversation
How are you actually setting the custom segments? Can you provide a code example? |
@taylorotwell thanks for answer protected function queueRecallerCookie(AuthenticatableContract $user) {
$this->getCookieJar()->queue($this->createRecaller(
$user->getAuthIdentifier().'|'.$user->getRememberToken().'|'.$user->getAuthPassword().
'|'.session('company_id', $this->request->input('company_id'))
));
}
protected function userFromRecaller($recaller){
$user = parent::userFromRecaller($recaller);
if ($user && $this->viaRemember && !($recaller->customSegments()[0] ?? false)) {
return;
}
$this->session->put(['company_id' => $recaller->customSegments()[0]]);
return $user;
} I can remove all the overwriting with this change, and use a middleware and an auth macro Maybe, is there a better option for this? |
How can you remove all that code? This PR gives no way to define custom segments - only retrieve them? |
yes, it is true, i think the same, but i'm trying to make the smaller change for the framework, i can remove the extending with an macro for renew the cookie, without this change it is imposible because if there is more than 3 segments "remember me" stop working( framework/src/Illuminate/Auth/Recaller.php Lines 80 to 84 in 544ee0d
|
Hmm, maybe just define the count of segments as a constant or a property in Recaller? This allow to extend it and do something like:
|
@korkoshko yes, it is what i have now, but i'm trying to avoid all the extending recaller.php, sessionguard.php, adding a service provider for the new guard, and others changes with this only change, i tested changing vendor and works |
@korkoshko you forget about overwriting also framework/src/Illuminate/Auth/Recaller.php Lines 80 to 84 in 544ee0d
count($segments) === 3 makes stop "remember me" on more than 4 segmentsAnd framework/src/Illuminate/Auth/Recaller.php Lines 50 to 53 in 544ee0d
There is a static 3 , that merge third segment with the fourth
|
@PaolaRuby When the count of segments will be constant or a property in Recaller (parent class), we won't need to override this method.
|
+1 for this
Would be great some changes on SessionGuard class like a segments resolver on login for the custom segments |
@korkoshko maybe, still, i'm waiting for Taylor, maybe he close this, the whole "extending" is what I'm trying to avoid |
@angeljqv @taylorotwell i can add a "session keys setter" on SessionGuard for define segments solution, like protected static $recallCustomSegments = [];
public function setRecallCustomSegments(array $keys){
self:$recallCustomSegments = $keys;
}
protected function queueRecallerCookie(AuthenticatableContract $user)
{
$this->getCookieJar()->queue($this->createRecaller(
$user->getAuthIdentifier().'|'.$user->getRememberToken().'|'.$user->getAuthPassword().$this->getCustomSegments()
));
}
protected function getCustomSegments()
{
$segments = [];
foreach (self::$recallCustomSegments as $segment) {
$segments[$segment] = $this->session->get($segment, '');
}
return $segments ? '|'.implode('|', $segments) : '';
}
// on userFromRecaller
if ($user && $this->viaRemember) {
$customSegments = $recaller->customSegments();
foreach (self::$recallCustomSegments as $i => $segment) {
$this->session->put([$segment => $customSegments[$i] ?? null]);
}
} now on auth service provider auth()->setRecallCustomSegments(['company_id']); Also i can add it from a config on |
@PaolaRuby btw, what if you need to change the company id for an already logged in user? |
@korkoshko just refresh the cookie with the new id, look at this for example auth()->getCookieJar()->queue(
auth()->getCookieJar()->make(auth()->getRecallerName(), 'pipe_segments', auth()->getRememberDuration())
); or with a macro, many ways |
Refactored this to just a plain |
@taylorotwell thank you very much, |
Closes #42271
Actually
Recaller
only admit 3 segments,id
,token
,hash
,If we create a custom session class extending
SessionGuard
, or we change theremember me
cookie on login, we need a way to get the extra data from recaller, this is useful when you have a multi company login, or when needs an extra id/value to rebuild the session,Example: company is a manyToMany relation with user, on login user pick wich company use for session, on "remember me" laravel rebuild the user session but not the company, of course i can create another cookie for rebuild the picked company or another company selector after session rebuild, but would be great if the recaller can handle that extra id/ids
keeps same functionality, but adds option for more segments, really small not breaking change
#42271 (comment)
Login, custom session guard, or auth macro
Rebuild the picked login company on session with remember me
User can be logged on two or more companies at the same time(using different browsers, or with incognito mode, different computers, etc)