How do I add more than one over method to a mojolicious route?

Viewed 1365

I have the following code:

$r->find('user')->via('post')->over(authenticated => 1);

Given that route I can get to the user route passing through the authenticated check that is setup using Mojolicious::Plugin::Authentication.

I want add another 'over' to that route.

$r->find('user')->via('post')->over(authenticated => 1)->over(access => 1);

That appears to override authenticated 'over' though.

I tried breaking up the routes with names like:

 my $auth = $r->route('/')->over(authenticated => 1)
     ->name('Authenticated Route');

 $access = $auth->route('/user')->over(access => 1)->name('USER_ACCESS');

That didn't work at all though. Neither of the 'over's are being accessed.

My routes are things like /user, /item, set up using MojoX::JSON::RPC::Service. So, I don't have things like /user/:id to set up sub routes.( not sure that matters ) All routes are like /user, sent with parameters.

I've got a condition like:

$r->add_condition(
    access => sub {
        # do some stuff
    },
);

that is the 'access' in $r->route('/user')->over(access => 1);

In short, the routes work fine when using:

$r->find('user')->via('post')->over(authenticated => 1);

But I'm unable to add a 2nd route.

So, what am I missing in setting up these routes with multiple conditions? Is it possible to add multiple conditions to a single route /route_name?

3 Answers

What if we use this approach?

# register condition
$r->add_condition(
    chain => sub {
        my ($route, $controller, $captures, $checkers) = @_; 

        for my $checker (@$checkers) {
            return 0 unless $checker->($route, $controller, $captures);
        }

        return 1;
    },
);

# ...
# example of using
$r->get('/')->over(chain => [\&checker1, \&checker2])->to(cb => \&foo)->name('bar');
Related