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

Access attributes in ignored paths #148

Open
simensol opened this issue Jan 25, 2019 · 2 comments
Open

Access attributes in ignored paths #148

simensol opened this issue Jan 25, 2019 · 2 comments
Labels

Comments

@simensol
Copy link

simensol commented Jan 25, 2019

Let's say I have a route \publicinfo that is accessible to both guests and registered users ('ignore' => ['/publicinfo']). How can I access the jwt attributes using $request->getAttribute("jwt") for registered users when they access \publicinfo? Since \publicinfo is added to ignore, the jwt attributes are never added to the $request object. However, if I remove \publicinfo from ignore, guests are not able to reach the route.

@tuupola
Copy link
Owner

tuupola commented Jan 29, 2019

You could remove /publicinfo from ignore and use northwoods/conditional-middleware to execute tuupola/slim-jwt-auth middleware only if request has a token.

@Frzk
Copy link

Frzk commented Apr 4, 2019

I did it another way:

Let your /publicinfo path in path so that the user has to have a valid JWT.

When a guest user tries to access it, the middleware will return a 401, which is expected.
The trick is to use the error option of the middleware to catch this situation and do whatever you want.

$app->add(new Tuupola\Middleware\JwtAuthentication([
    "path" => ["/publicinfo"],
    "error" => function($response, $arguments) use ($container) {
        if ($response->getStatusCode() === 401) {
            // The user is NOT authenticated, maybe display the login form:
            $response = $container->get('renderer')->render($response, 'publicinfo.html');
        } else {
            // Another error happened. Display an error message.
            $response = $container->get('renderer')->render($response, 'error.html');
        }

        return $response;
    },
]));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants