How to change error messages from Passport OAuth in Laravel 5.3?

I am using Laravel 5.3 for the REST server that the mobile application is connecting to. I already set up an exception handler for most Laravel errors and worked the way I like.

However, OAuth errors return ugly. Example:

{ "error":"invalid_credentials", "message":"The user credentials were incorrect." } 

Apparently, I'm not the only one bothered by the underlined error code β€” I only have a few beta testers, and they have already reported this as an β€œerror.”

The passport seems to bypass the regular error handler and pass through League\OAuth2\Server\Exception\OAuthServerException . These ugly error messages are hardcoded to this file.

 public static function invalidCredentials() { return new static('The user credentials were incorrect.', 6, 'invalid_credentials', 401); } 

I seriously tried, like a dozen approaches to extending / overriding OAuthServerException, but I just can't get it to work.

+5
source share
1 answer

To change the error messages from the Passport, one way would be to extend the Passport base classes and similar OAuthException methods as you tried. Here is a reasonable explanation of how to do this. I tried to do this for something similar, but in my case I found it to be redundant and went something simpler.

However, if the problem is how you display the error in your mobile application, one approach could be to compare these errors with something more desirable, rather than using them directly to tell your users (the following pseudocode is like I "m not sure if your mobile application is encoded):

 function handleErrorResponse(response) { var errorMap = { "invalid_credentials": "Username or password was incorrect." }; return errorMap[response.error]; } 

Thus, League \ OAuth2 \ Server is your source of truth, and your application simply translates these basic messages when reporting to the user. Hope this helps!

0
source

Source: https://habr.com/ru/post/1262816/


All Articles