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] Updates the Exception Handler in order to allow custom AuthenticationException message #21575

Merged
merged 1 commit into from
Oct 9, 2017

Conversation

aocneanu
Copy link
Contributor

@aocneanu aocneanu commented Oct 7, 2017

Before the PR, in the handler the custom message is overwritten before returning the response.

Example use case: the user account gets disabled while the user is logged in the app and we want to logout the user and throw an exception with an appropriate message.

…Exception message.

Example use case: the user account gets disabled while the user is logged in the app and we want to logout the user and throw an exception with an appropriate message.
Before the PR, in the handler the custom message is overwritten before sending the response.
@@ -216,7 +216,7 @@ protected function prepareException(Exception $e)
protected function unauthenticated($request, AuthenticationException $exception)
{
return $request->expectsJson()
? response()->json(['message' => 'Unauthenticated.'], 401)
? response()->json(['message' => $exception->getMessage()], 401)
Copy link
Member

Choose a reason for hiding this comment

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

Probably should be $exception->getMessage() ?: 'Unauthenticated.'?

Copy link
Contributor Author

@aocneanu aocneanu Oct 7, 2017

Choose a reason for hiding this comment

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

I think it's not necessary because the constructor for AuthenticationException already has a default value for the $message property

namespace Illuminate\Auth;
use Exception;
class AuthenticationException extends Exception
...
public function __construct($message = 'Unauthenticated.', array $guards = [])
{
    parent::__construct($message);

    $this->guards = $guards;
}
...

Copy link
Member

Choose a reason for hiding this comment

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

I think it's not necessary because the constructor for AuthenticationException already has a default value for the $message property

And what if someone actuallty constructs it with somethinh empty?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Something empty like:

throw new AuthenticationException('') ?

If this is the case I would expect that this person does not want any message at all.

Or you were thinking of something else?

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm with @aocneanu on this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also, while inspecting a little more I found that this exception is used in 2 middlewares and one trait:

  • Authenticate middleware
    Here the exception is thrown using "Unauthenticated." message.

  • AuthenticateSession middleware
    thrown without a message.

  • GuardHelpers trait
    thrown without a message.

If we want to be consistent we could either

a) hardcode the "Unauthenticated." message in AuthenticateSession middleware and in GuardHelpers trait, and maybe remove the default message from the AuthenticationException constructor. In this case, in Handler we could put the an elvis operator if we consider that the user never wants to throw this exception without a message, or a ternary and cover both situations

b) remove the "Unauthenticated." message from the Authenticate middleware and keep the default value in the AuthenticationException constructor.

If you find any of this suitable let me know and I will commit again.

@GrahamCampbell GrahamCampbell changed the title Updates the Exception Handler in order to allow custom AuthenticationException message [5.5] Updates the Exception Handler in order to allow custom AuthenticationException message Oct 7, 2017
@aocneanu
Copy link
Contributor Author

aocneanu commented Oct 7, 2017

@taylorotwell can you please tell me what is wrong with the pull request?

@taylorotwell taylorotwell reopened this Oct 7, 2017
@aocneanu
Copy link
Contributor Author

aocneanu commented Oct 8, 2017

What’s next? Am I supposed to do something? If so please let me know.

@taylorotwell taylorotwell merged commit 39a4b32 into laravel:5.5 Oct 9, 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