EDIT: It's in the manual already. Catalyst-Manual

Lets start with the database. I usually write my schema from scratch and deploy it with a script into the database of choice.

MyApp/lib/MyApp/Schema/Result/Users.pm

package MyApp::Schema::Result::Users;

__PACKAGE__->load_components(qw/
            EncodedColumn
            Core
/);

__PACKAGE__->table("users");
__PACKAGE__->add_columns(
    ...
    "password",
    {
        data_type => "char",
        is_nullable => 0,
        size => 40 + 10,
        encode_column => 1,
        encode_class => 'Digest',
        encode_args => {algorithm => 'SHA-1', format => 'hex', salt_length => 10},
        encode_check_method => 'check_password',
    },
);

Next thing to do is to configure the application.

MyApp/lib/MyApp.pm

package MyApp;
...
__PACKAGE__->config(
    'Plugin::Authentication' => {
        default => {
            credential => {
                class => 'Password',
                password_field => 'password',
                password_type => 'self_check',
            },
            store => {
                class => 'DBIx::Class',
                user_model => 'DB::Users',
            },
        },
    }
);

Now for the Controller. I have my own 'Users' Controller

MyApp/lib/MyApp/Controller/Users.pm

package MyApp::Controller::Users;
use Moose;
use namespace::autoclean;
BEGIN {extends 'Catalyst::Controller::HTML::FormFu'; }

sub register :Path('/register') :Args(0) :FormConfig {
    my ( $self, $c ) = @_;
    my $form = $c->stash->{form};
    if ($form->submitted_and_valid) {
        my $user = $c->model('DB::Users')->create({
            username  => $form->params->{'username'},
            password  => $form->params->{'password'},
        });
    } else {
        ...
    }
}

sub login :Path('/login') :Args(0) {
    my ($self, $c) = @_;
    if($c->authenticate({
        username => $c->req->params->{username},
        password => $c->req->params->{password},
    })) {
        $c->flash(msg => 'You are logged in.');
        $c->res->redirect($c->uri_for($c->controller('Root')->action_for('index')));
    } else {
        $c->flash(error => 'Wrong username/password combination.');
        $c->res->redirect($c->uri_for($self->action_for('login')));
    }
}

and my FormFu config

MyApp/root/forms/users/register.yml

indicator: submit
elements:

    - type: Text
      name: 'username'
      label: 'username'

    - type: Password
      name: 'password'
      label: 'password'
      constraints:
        - type: Equal
          others: repeat_password

    - type: Password
      name: 'repeat_password'
      label: 'repeat password'

    - type:  Submit
      name:  submit
      value: Submit
My tags:
 
Popular tags:
  Authentication EncodedColumn
Powered by Catalyst
Powered by MojoMojo Hosted by Shadowcat - Managed by Nordaaker