A piggy bank of commands, fixes, succinct reviews, some mini articles and technical opinions from a (mostly) Perl developer.

Jump to

Quick reference

How to do authentication with Mojolicious

It's like this:


package AuthenticatedFoo;
use Mojo::Base 'Mojolicious';
sub setup_routes {
my ($self) = @_;
my $r = $self->routes;
my $auth = \&authenticate;
$r->under($auth)->route('/test')->via('GET')->to( controller => 'Test', action => 'test' );
}
sub authenticate {
my ($c) = @_;
# Skip authentication if not enabled
return 1 if ! $c->config->{auth}{enabled};
# Validate auth token if possible
my $validated_ref = Bar::Auth->validate(jwt => '...', public_key => '...');
return 1 if $validated_ref;
# Not authorised
$c->render(text => "Request not authorised", status => 401);
return undef;
}