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

Added middleware #490

Merged
merged 12 commits into from
Sep 7, 2023
Merged

Added middleware #490

merged 12 commits into from
Sep 7, 2023

Conversation

JVT038
Copy link
Collaborator

@JVT038 JVT038 commented Sep 3, 2023

While the name of this branch is 'add-web-authentication-middleware', this PR also adds a bunch of other middleware outside of authentication (such as checking whether the user has a Jellyfin / Plex token or is an Admin, etc.)

The middleware stuff is in src/HttpController/Middleware.

The new syntax for a route is as follows:
$routeCollector->addRoute('GET', '<URI>', [[<Class\SomeHandler::class>, '<someMethod>'], 'middleware' => [<someMiddleware::class>,<someOtherMiddleware::class>]]);.

I'll try and see if I can improve the syntax, because it's currently quite ugly IMO. If no middleware is necessary, the old syntax can be used fine; so then the handler shouldn't be in an array.

@JVT038
Copy link
Collaborator Author

JVT038 commented Sep 3, 2023

Also, I made some new files in the Service directory; that's my attempt at extending the Fastroute library and trying to improve the syntax with some addMiddleware method. It can be ignored for now, because it doesn't work at all (and it's not used either).

@JVT038
Copy link
Collaborator Author

JVT038 commented Sep 4, 2023

So I managed to improve the syntax a bit. This is how it currently looks like:

$routes->createNewRoute('GET', '/', [Some\Controller::class, 'callbackfunction'])->addMiddleware(Middleware\SomeMiddleware::class, Middleware\SomeOtherMiddleware::class);

@JVT038 JVT038 marked this pull request as ready for review September 4, 2023 15:32
@JVT038 JVT038 requested a review from leepeuker as a code owner September 4, 2023 15:32
@JVT038
Copy link
Collaborator Author

JVT038 commented Sep 4, 2023

I'm not sure if we should replace this check with a middleware:

        $userId = $this->userPageAuthorizationChecker->findUserIdIfCurrentVisitorIsAllowedToSeeUser((string)$request->getRouteParameters()['username']);
        if ($userId === null) {
            return Response::createNotFound();
        }

The $userId variable is used often and if we were going to replace this with a middleware, the controller would still need the userId and the check method findUserIdIfCurrentVisitorIsAllowedToSeeUser() won't be removed. Wouldn't it be unnecessary to replace this thing?

@leepeuker leepeuker linked an issue Sep 7, 2023 that may be closed by this pull request
@leepeuker leepeuker merged commit 1821f97 into main Sep 7, 2023
@leepeuker leepeuker deleted the add-web-authentication-middleware branch September 7, 2023 13:52
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.

Add middleware to routes
2 participants