Created
January 19, 2017 09:24
-
-
Save brianmed/329600b608d5cd84808034cac0532153 to your computer and use it in GitHub Desktop.
Mojolicious Under app 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
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