0

I'm trying to use DBIx::Class for authentication users from Catalyst app. My steps I've done:

1) created SQLite db

CREATE TABLE people (
id integer primary key,
name text not null,
password text not null);

2) created Catalyst model People;

3) set up auth config in MyApp.pm

__PACKAGE__->config('Plugin::Authentication' =>     {   
default => {    credential => {
                class => 'Password',
                password_field => 'password',
                password_type => 'clear' 
                    },          
                    store => {
                class => 'DBIx::Class',
                user_model => 'People'          
                    }
                } 
    }
);

4) Created controller Auth and set method login in it:

sub login : Local {
   my ($self, $c) = @_;

   if (my $user = $c->req->params->{user} and my $password = $c->req->params->{password} ) {
       if ( $c->authenticate( { username => $user, password => $password } ) ) {
           $c->res->body( "hello " . $c->user->get("id") );
       } else {
           # login incorrect
           $c->res->body("Wrong pass or name!");
       }
   } else {
       # invalid form input
       $c->res->body("Type name & pass");
   }
}

5) Called method login when a form with user and password data is submitted. And I got this message:

Caught exception in MyApp::Controller::Auth->login "Can't locate object method "result_source" via package "MyApp::Model::People" at /usr/local/share/perl/5.14.2/Catalyst/Authentication/Store/DBIx/Class/User.pm line 35, line 999."

How can it be fixed?

SnareChops
  • 13,175
  • 9
  • 69
  • 91
edem
  • 3,222
  • 3
  • 19
  • 45

3 Answers3

0

The Catalyst::Authentication::Store::DBIx::Class docs state that the user_model has to be full-qualified, MyApp::ModelName::People in your case.

Also read the Catalyst::Model::DBIC::Schema docs about how to integrate a DBIx::Class schema into a Catalyst application as a model.

Alexander Hartmaier
  • 2,178
  • 12
  • 21
  • "`store => { class => 'DBIx::Class',user_model => 'MyApp::Users', role_column => 'roles',} } } );` The authentication system works behind the scenes to load your data from the new source. The rest of your application is completely unchanged." This is from Catalyst::Plugin::Authentication's man. – edem Feb 04 '13 at 20:45
0

There is strange behave of Catalyst helper when use it to create model DBIC. If the model name and table name the same then helper will create a Result class with other name. Not the name of database table but it will be another one. Therefore is needed to use the name of Result class with name of Model like this: store => { class => 'DBIx::Class', user_model => 'People::Person' }

P.S. Person name was chosen automatically by Catalyst helper.

edem
  • 3,222
  • 3
  • 19
  • 45
0

@edem I think, it's because you have not created people.pm result class and defined the table structure.

NullException
  • 4,232
  • 8
  • 24
  • 44