Skip to content

Instantly share code, notes, and snippets.

@brianmed
Created January 19, 2017 09:24
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 brianmed/329600b608d5cd84808034cac0532153 to your computer and use it in GitHub Desktop.
Save brianmed/329600b608d5cd84808034cac0532153 to your computer and use it in GitHub Desktop.
Mojolicious Under app example
package Under;
use Mojo::Base 'Mojolicious';
use Mojo::Util qw(secure_compare);
sub startup {
my $self = shift;
my $r = $self->routes;
$r->get('/', sub {
my $c = shift;
$c->render(text => 'Hello World!');
});
my $logged_in = $r->under (sub {
my $c = shift;
# Have access
return 1 if secure_compare(
$c->req->url->to_abs->userinfo,
'user:523487063f1011e68442002500f18b6d'
);
# Do not have access
$c->render(json =>
{status => "error", data => { message => "Credentials mis-match" }}
);
return undef;
});
$logged_in->get('/admin', sub {
my $c = shift;
$c->render(text => 'User access area');
});
}
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment