Skip to content

Instantly share code, notes, and snippets.

@jberger
Last active December 16, 2015 00:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jberger/5349617 to your computer and use it in GitHub Desktop.
Save jberger/5349617 to your computer and use it in GitHub Desktop.
Under translation example
=pod
Say we have some route that depends on query parameters for a rudimentary
authentication.
my %users = ( Bender => 'ByteMe' );
under '/protected' => sub {
my $self = shift;
my $user = $self->param('user');
my $pass = $self->param('pass');
return $pass eq $users{$user};
};
# /protected/hi?user="Bender"&pass="ByteMe" ---> Welcome in!
any '/hi' => sub { shift->render( text => 'Welcome in!') };
In a full app, the C<under> method returns a new router which should be used
to handle the requests which should be protected. Therefore the above
example translates as
my %users = ( Bender => 'ByteMe' );
sub startup {
my $app = shift;
my $r = $app->routes;
my $protected = $r->under( '/protected' => sub {
my $self = shift;
my $user = $self->param('user');
my $pass = $self->param('pass');
return $pass eq $users{$user};
});
# /protected/hi?user="Bender"&pass="ByteMe" ---> Welcome in!
$protected->any( '/hi' => sub { shift->render( text => 'Welcome in!') });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment