Last active
December 16, 2015 00:39
-
-
Save jberger/5349617 to your computer and use it in GitHub Desktop.
Under translation example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
=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