tuupola / slim-jwt-auth

PSR-7 and PSR-15 JWT Authentication Middleware
https://appelsiini.net/projects/slim-jwt-auth
MIT License
826 stars 141 forks source link

Translate error messages #153

Open jpaiardi opened 5 years ago

jpaiardi commented 5 years ago

Hi, Is it possible to modify (translate) the error messages that are generated, for example when not receiving a token?

tuupola commented 5 years ago

Not directly, but you could use the error handler. Let's assume you have created a translating function translate_error($locale, $message) then the error handler would be something like:

$app = new Slim\App;

$app->add(new Tuupola\Middleware\JwtAuthentication([
    "secret" => "supersecretkeyyoushouldnotcommittogithub",
    "error" => function ($response, $arguments) {
        $data["status"] = "error";
        $data["message"] = translate_error("et_EE", $arguments["message"]);
        return $response
            ->withHeader("Content-Type", "application/json")
            ->getBody()->write(json_encode($data, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT));
    }
]));
thepercival commented 4 years ago

Problem with translating the message, is that when the message in english changes, there will be no translation and the developer will not know. When a token expires Firebase\JW\ExpiredException is thrown. From a user(other developer)-view would it not be better to be able to catch the Firebase\JW\ExpiredException instead of handling the "error"-function( "error" => function ($response, $arguments) { ... } )?

I have been using your library for a few years now and I want to thank you for the effort and functionality btw.