[8.x] Adds attemptWith()
with tests
#37090
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR expands on the discussion here about adding callbacks the the authentication attempt mechanism.
Problem
There is no way to add additional checks at runtime when using
attempt()
. Any addition forces the developer to create a new Guard, or use nonchalantlyvalidate()
andgetLastAttempted()
, and fireAttempting
andFailed
manually.As you can see, this whole mess becomes spaghetti (yum!) code quickly.
Solution
This PR adds the
SessionGuard::attemptWith()
method. It's a carbon copy of the originalattempt()
method, but receives a third argument of one or multiple callbacks. After the user is validated, each of these run an additional check by receiving the validated User. If one callback returns false, the whole attempt fails.Since there are
callables
, the developer can add its own checks, or use checks made by external packages, all while controlling manually the flow and order of these.How it works
The change adds the
attemptWith()
and the internal methodshouldLogin()
. The latter executes each callback and returnsfalse
if one of them "fails". This is called after the user is properly validated:Since it's a new method, no breaking changes are in sight.