benvanstaveren / Mojolicious-Plugin-Authentication

A plugin to make authentication a bit easier
http://search.cpan.org/dist/Mojolicious-Plugin-Authentication/
Other
20 stars 17 forks source link

Can't locate object method "current_user" #21

Closed pavelsr closed 8 years ago

pavelsr commented 8 years ago

Hello again, Maybe i did something wrong, maybe it's a bug, I don't know, there is no documentation again :)

I'm trying to output in a template user_id.

Example.pm (package MongoPA::Controller::Example):

sub index {
  my $self = shift;
  $self->render(user => $self->current_user);
}

index.html.ep:

<p>User ID:<%= $user %></p>

database structure:

> db.users.find()
{ "_id" : ObjectId("574ffd0cf5bef13ebeecd0bf"), "login" : "demo", "user_id" : 1, "password" : "demo" }
{ "_id" : ObjectId("574ffd27436a1b4f9c6f47bb"), "login" : "demo2", "user_id" : 2, "password" : "demo2" }

MongoPA.pm:

...
 $self->plugin('authentication' => {
    'autoload_user' => 1,
    'load_user' => sub {  
        my ($app, $uid) = @_;
        $app->session->{'user_id'} = $uid;
        return {
            'id'     => $uid
        } if $uid;
        return undef;
    },
    'validate_user' => sub {
        my ($app, $username, $password, $extradata) = @_;   
        $app->collection('users')->find_one({login => $username} => sub {
            my ($collection, $err, $doc) = @_;
            if (ref($doc) eq 'HASH') {
                if ($doc->{password} eq $password) {
                    return $doc->{user_id};
                } 
            }
        return undef;
        });
        Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
       },
    'current_user_fn' => 'user', # compatibility with old code
  });
...
 my $auth_bridge = $r->under('/')->to('auth#check');
 $auth_bridge->get('/')->to('example#index');

but it gives me following error:

Can't locate object method "current_user" via package "MongoPA::Controller::Example"

Authorization is working, problem is only output of user_id. According docs current user() method returns the user object as it was returned from the supplied load_user subroutine ref. In my case it must be hash like { 'id' =>1 }. But in fact I got an error. Don't know what to do.

benvanstaveren commented 8 years ago

Hi Pavel,

That does look like a bug, the current_user() helper should be there - there is a way to tell the module to use a different helper name, could you try that (e.g. name it 'test_current_user' or something) and see if that works? On 06/02/2016 12:49 PM, Pavel Serikov wrote:

Hello again, Maybe i did something wrong, maybe it's a bug, I don't know, there is no documentation again :)

I'm trying to output in a template user_id.

Example.pm (package MongoPA::Controller::Example):

sub index { my $self = shift; $self->render(user => $self->current_user); }

index.html.ep:

User ID:<%= $user %>

database structure:

|> db.users.find() { "_id" : ObjectId("574ffd0cf5bef13ebeecd0bf"), "login" : "demo", "user_id" : 1, "password" : "demo" } { "_id" : ObjectId("574ffd27436a1b4f9c6f47bb"), "login" : "demo2", "user_id" : 2, "password" : "demo2" } |

MongoPA.pm:

... $self->plugin('authentication' => { 'autoload_user' => 1, 'loaduser' => sub {
my ($app, $uid) = @
; $app->session->{'user_id'} = $uid; return { 'id' => $uid } if $uid; return undef; }, 'validateuser' => sub { my ($app, $username, $password, $extradata) = @;
$app->collection('users')->findone({login => $username} => sub { my ($collection, $err, $doc) = @; if (ref($doc) eq 'HASH') { if ($doc->{password} eq $password) { return $doc->{user_id}; } } return undef; }); Mojo::IOLoop->start unless Mojo::IOLoop->is_running; }, 'current_user_fn' => 'user', # compatibility with old code }); ... my $auth_bridge = $r->under('/')->to('auth#check'); $auth_bridge->get('/')->to('example#index');

but it gives me following error:

|Can't locate object method "current_user" via package "MongoPA::Controller::Example" |

Authorization is working, problem is only output of user_id. According docs https://metacpan.org/pod/Mojolicious::Plugin::Authentication#current_user current user() method returns the user object as it was returned from the supplied load_user subroutine ref. In my case it must be hash like { 'id' =>1 }. But in fact I got an error. Don't know what to do.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/benvanstaveren/Mojolicious-Plugin-Authentication/issues/21, or mute the thread https://github.com/notifications/unsubscribe/AAQxlJLBRpRlDs1nrszhZ7gJj463FmQFks5qHrVPgaJpZM4IsboT.

benvanstaveren commented 8 years ago

Ah wait, I see the problem :D

See the 'current_user_fn' value in the plugin config in your source below, that sets the helper name, so instead of having "current_user", you have it set to "user" - so doing:

$self->render(user => $self->user)

Would work :)

On 06/02/2016 12:49 PM, Pavel Serikov wrote:

Hello again, Maybe i did something wrong, maybe it's a bug, I don't know, there is no documentation again :)

I'm trying to output in a template user_id.

Example.pm (package MongoPA::Controller::Example):

sub index { my $self = shift; $self->render(user => $self->current_user); }

index.html.ep:

User ID:<%= $user %>

database structure:

|> db.users.find() { "_id" : ObjectId("574ffd0cf5bef13ebeecd0bf"), "login" : "demo", "user_id" : 1, "password" : "demo" } { "_id" : ObjectId("574ffd27436a1b4f9c6f47bb"), "login" : "demo2", "user_id" : 2, "password" : "demo2" } |

MongoPA.pm:

... $self->plugin('authentication' => { 'autoload_user' => 1, 'loaduser' => sub {
my ($app, $uid) = @
; $app->session->{'user_id'} = $uid; return { 'id' => $uid } if $uid; return undef; }, 'validateuser' => sub { my ($app, $username, $password, $extradata) = @;
$app->collection('users')->findone({login => $username} => sub { my ($collection, $err, $doc) = @; if (ref($doc) eq 'HASH') { if ($doc->{password} eq $password) { return $doc->{user_id}; } } return undef; }); Mojo::IOLoop->start unless Mojo::IOLoop->is_running; }, 'current_user_fn' => 'user', # compatibility with old code }); ... my $auth_bridge = $r->under('/')->to('auth#check'); $auth_bridge->get('/')->to('example#index');

but it gives me following error:

|Can't locate object method "current_user" via package "MongoPA::Controller::Example" |

Authorization is working, problem is only output of user_id. According docs https://metacpan.org/pod/Mojolicious::Plugin::Authentication#current_user current user() method returns the user object as it was returned from the supplied load_user subroutine ref. In my case it must be hash like { 'id' =>1 }. But in fact I got an error. Don't know what to do.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/benvanstaveren/Mojolicious-Plugin-Authentication/issues/21, or mute the thread https://github.com/notifications/unsubscribe/AAQxlJLBRpRlDs1nrszhZ7gJj463FmQFks5qHrVPgaJpZM4IsboT.

pavelsr commented 8 years ago

@benvanstaveren, got it, you are absolutely right! Or another way - remove following string

'current_user_fn' => 'user', # compatibility with old code

and use $self->current_user :)