Skip to content
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

[5.5] Set current user before firing authenticated event #21790

Merged
merged 1 commit into from
Oct 25, 2017

Conversation

imbrish
Copy link
Contributor

@imbrish imbrish commented Oct 23, 2017

When SessionGuard retrieves currently authenticated user it stores the instance only after handling the Authenticated event. This leads to an infinite loop if any event handler happens to call Auth::user().

This PR fixes the described problem. It also makes the behavior of SessionGuard::user() method consistent with SessionGuard::setUser(), in which user instance is stored before firing the event.

@taylorotwell taylorotwell requested a review from themsaid October 23, 2017 17:35
Copy link
Member

@themsaid themsaid left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks ok to me

@sisve
Copy link
Contributor

sisve commented Oct 23, 2017

One behavior change is that you could previously throw exception in the authenticated/login events, and the user wouldn't be logged in. This new logic means the user is logged in ($this->user is set) even if an authenticated/login event threw an exception. The workaround would be to encapsulate the logic in a try/catch and set $this->user = $oldValue; if an exception is thrown. Note that it has to be the old value of $this->user, before the try/catch, not null. The old behavior of the user() method would leave $this->user unchanged until everything has succeeded.

@themsaid
Copy link
Member

@sisve I thought of that but couldn't think of it as breaking since the user is already logged in, the event is just declaring this fact so it's expected that a user is authenticated when the authenticated event is fired.

@imbrish
Copy link
Contributor Author

imbrish commented Oct 24, 2017

@sisve, I don't see why someone would choose this method of verifying session/cookies. It is also beyond me in what circumstances you would juggle between multiple authenticated users during a single request.

If you throw an exception then request is over anyway. If you catch it... that's a pretty weird if for me.

@taylorotwell taylorotwell merged commit cfd290f into laravel:5.5 Oct 25, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants