Skip to content

Instantly share code, notes, and snippets.

@hesco
Last active January 24, 2018 03:36
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 hesco/6e05694f35a733fd36b9279dfd01236c to your computer and use it in GitHub Desktop.
Save hesco/6e05694f35a733fd36b9279dfd01236c to your computer and use it in GitHub Desktop.
Earlier, I was trying to get a Mojolicious Plugin working inside a Moose::Role. At the moment, I'd be happy to get this plugin working in a `mojo generate app MyApp` scaffolding. Any guidance would be appreciated. Has the Mojolicious::Plugin::Authentication module been seen to work in the real world? This is my first time reaching for it.
mkdir foo
cd foo
echo "requires 'Mojolicious', '7.33';" > cpanfile
echo "requires 'Mojolicious::Plugin::Authentication', '1.32';" >> cpanfile
carton install
perl -Ilocal/lib/perl5 local/bin/mojo generate app MyApp
mv cpanfile* local/ my_app/
cd my_app/
vim lib/MyApp.pm
perl -wc -Ilocal/lib/perl5 lib/MyApp.pm
vim t/basic.t
$ prove -v t/basic.t 2> /dev/null
t/basic.t ..
ok 1 - GET /
ok 2 - 200 OK
ok 3 - content is similar
ok 4 - An object of class 'MyApp' isa 'MyApp'
not ok 5 - MyApp->can('authenticate')
not ok 6 - MyApp->can('is_user_authenticated')
not ok 7 - MyApp->can('current_user')
not ok 8 - MyApp->can('reload_user')
not ok 9 - MyApp->can('signature_exists')
not ok 10 - MyApp->can('logout')
1..10
Dubious, test returned 6 (wstat 1536, 0x600)
Failed 6/10 subtests
Test Summary Report
-------------------
t/basic.t (Wstat: 1536 Tests: 10 Failed: 6)
Failed tests: 5-10
Non-zero exit status: 6
Files=1, Tests=10, 1 wallclock secs ( 0.04 usr 0.00 sys + 0.44 cusr 0.06 csys = 0.54 CPU)
Result: FAIL
$ cat t/basic.t
#!/usr/bin/env perl
use lib qw( lib local/lib/perl5 );
use Mojo::Base -strict;
use Test::More;
use Test::Mojo;
use Data::Dumper;
my $t = Test::Mojo->new('MyApp');
$t->get_ok('/')->status_is(200)->content_like(qr/Mojolicious/i);
isa_ok( $t->app, 'MyApp' );
can_ok( $t->app, 'authenticate' );
can_ok( $t->app, 'is_user_authenticated' );
can_ok( $t->app, 'current_user' );
can_ok( $t->app, 'reload_user' );
can_ok( $t->app, 'signature_exists' );
can_ok( $t->app, 'logout' );
diag( Dumper $t->app );
done_testing();
package MyApp;
use Mojo::Base 'Mojolicious';
use Mojolicious::Plugin::Authentication;
# This method will run once at server start
sub startup {
my $self = shift;
# Load configuration from hash returned by "my_app.conf"
my $config = $self->plugin('Config');
# Documentation browser under "/perldoc"
$self->plugin('PODRenderer') if $config->{perldoc};
$self->plugin( 'authentication' => {
autoload_user => 1,
session_key => 'gagp_api',
load_user => sub {
my $app = shift;
my $uid = shift;
my $user = { uid => 1, username => 'flhammer', foo => 'bar' };
return $user;
},
validate_user => sub {
my $app = shift;
my $username = shift;
my $password = shift;
my $extradata = shift;
my $uid = 1;
return $uid;
},
current_user_fn => 'user', # compatibility with old code
});
# Router
my $r = $self->routes;
# Normal route to controller
$r->get('/')->to('example#welcome');
}
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment