Skip to content

Instantly share code, notes, and snippets.

@jjn1056
Last active October 8, 2021 15:18
Show Gist options
  • Save jjn1056/5fc02f28672c75c984dfc11bfa257a64 to your computer and use it in GitHub Desktop.
Save jjn1056/5fc02f28672c75c984dfc11bfa257a64 to your computer and use it in GitHub Desktop.
package Example::Controller::Root;
use Moose;
use MooseX::MethodAttributes;
extends 'Catalyst::Controller';
sub root :Chained(/) PathPart('') CaptureArgs(0) { }
# map body params directly to action args
sub login :Chained(root) PathPart(login) Args(0) Body(username Str, password Str) ($self, $c, $username, $password)
{
my $error = '';
if($c->req->method eq 'POST') {
$c->redirect_to_action('home') if $c->authenticate({
username=>$username,
password=>$password,
});
$error = 'User not found!';
}
$c->stash(error => $error);
}
# map body params to model 'LoginCredentials', which does an expected role
sub login :Chained(root) PathPart(login) Args(0) PostParams(credentials LoginCredentials) ($self, $c, $credentials)
{
my $error = '';
if($c->req->method eq 'POST') {
$c->redirect_to_action('home') if $c->authenticate({
username=>$credentials->username,
password=>$credentials->password,
});
$error = 'User not found!';
}
$c->stash(error => $error);
}
sub end : ActionClass('RenderView') {}
__PACKAGE__->config(namespace=>'');
__PACKAGE__->meta->make_immutable;
package Example::Model::LoginCredentials;
use Moose;
use CatalystX::Injection;
extends 'Catalyst::Model';
has 'username' => (is=>'ro', required=>1, body_param=>1);
has 'password' => (is=>'ro', required=>1, body_param=>1);
__PACKAGE__->meta->make_immutable;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment