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?
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.