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

Validate_user returns user_id = 2, but load_user somehow receive as input user_id = 1 #22

Closed pavelsr closed 7 years ago

pavelsr commented 8 years ago

Hi,

$self->plugin('authentication' => {
    'autoload_user' => 1,
    'load_user' => sub {  
        my ($app, $uid) = @_;
        $app->app->log->info("Created a session uid : ".$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) {
                    $app->app->log->info("Prepare to pass to load_user uid = ".$doc->{user_id});
                    return $doc->{user_id};
                } 
            }
        return undef;
        });
        Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
       },
    'current_user_fn' => 'user', # compatibility with old code
  });

In Mojolicious log I have:

[Sat Jun  4 13:13:15 2016] [info] Prepare to pass to load_user uid = 2
[Sat Jun  4 13:13:15 2016] [info] Created a session for user_id : 1

And template

<p>User_ID : <%= session 'auth_data' %></p>

renders like: User ID: 1

How it's possible?

benvanstaveren commented 8 years ago

Probably the asynchronous attempt at loading things, it's the one thing this module doesn't really deal well with, I haven't been able to duplicate it, lacking a mongodb installation right now but I'll take a look and see what I can do :)

jjatria commented 7 years ago

@pavelsr, could you provide (maybe through a gist?) a full minimal working example that illustrates this problem?

jjatria commented 7 years ago

I will close this for lack of activity, but if the problem persists with a more recent version, please feel free to open the issue again, or submit a new ticket.