#
__PACKAGE__->config(namespace => '');
+use Chatty::Form::Login;
+use Chatty::Form::Register;
+
+has 'login_form' => (
+ isa => 'Chatty::Form::Login',
+ is => 'rw',
+ lazy => 1,
+ default => sub { Chatty::Form::Login->new }
+);
+
+has 'register_form' => (
+ isa => 'Chatty::Form::Register',
+ is => 'rw',
+ lazy => 1,
+ default => sub { Chatty::Form::Register->new }
+);
+
=head1 NAME
Chatty::Controller::Root - Root Controller for Chatty
sub login :Local :Args(0) {
my ($self, $c) = @_;
- if ($c->req->method eq 'POST' && exists($c->req->params->{handle})) {
- eval {
- if ($c->authenticate({
- username => $c->req->params->{handle},
- password => $c->req->params->{password}
- })) {
- $c->change_session_id;
- my $user = $c->user->get('username');
- $c->flash->{message} = "Hi, $user! You are now logged in.";
- $c->response->redirect($c->uri_for('/'));
- }
- else {
- $c->flash->{error} = "Log-in failed! Try again, I guess.";
- $c->response->redirect($c->uri_for('login'));
- }
+
+ $c->stash(form => $self->login_form);
+ $self->login_form->process($c->req->params);
+ return unless $self->login_form->is_valid;
+
+ eval {
+ if ($c->authenticate({
+ username => $self->login_form->value->{username},
+ password => $self->login_form->value->{password}
+ })) {
+ $c->change_session_id;
+ my $user = $c->user->get('username');
+ $c->flash->{message} .= "Hi, $user! You are now logged in.";
+ $c->response->redirect($c->uri_for('/'));
+ }
+ else {
+ $c->flash->{error} = "Log-in failed! Try again, I guess.";
+ $c->response->redirect($c->uri_for('login'));
}
}
}
=cut
sub register :Local :Args(0) {
+ my ($self, $c) = @_;
+
+ $c->stash(form => $self->register_form);
+
+ my $new_account = $c->model('DB::Account')->new_result({});
+ $self->register_form->process(
+ item => $new_account,
+ params => $c->req->params
+ );
+
+ return unless $self->register_form->is_valid;
+
+ $c->flash->{message} = "Registration complete. ";
+ $c->forward('login');
}
=head2 default
--- /dev/null
+package Chatty::Form::Login;
+
+use HTML::FormHandler::Moose;
+extends 'HTML::FormHandler';
+
+has_field 'username' => (label => 'Username', required => 1);
+has_field 'password' => (type => 'Password', required => 1);
+has_field 'submit' => (type => 'Submit', value => 'Login');
+
+no HTML::FormHandler::Moose;
+__PACKAGE__->meta->make_immutable;
+1;
--- /dev/null
+package Chatty::Form::Register;
+
+use HTML::FormHandler::Moose;
+extends 'HTML::FormHandler::Model::DBIC';
+
+has '+item_class' => (default => 'Account');
+
+has_field 'email' => (type => 'Email', label => 'Email address');
+has_field 'username' => (label => 'User Nickname', required => 1, unique => 1);
+has_field 'password' => (type => 'Password', required => 1);
+has_field 'password_confirm' => (type => 'PasswordConf', required => 1);
+has_field 'submit' => (type => 'Submit', value => 'Register');
+
+has '+unique_messages' => (default => sub {
+ {username => 'Username is already registered'};
+ });
+
+no HTML::FormHandler::Moose;
+__PACKAGE__->meta->make_immutable;
+1;
use namespace::autoclean;
extends 'DBIx::Class::Core';
-__PACKAGE__->load_components("InflateColumn::DateTime");
+__PACKAGE__->load_components("InflateColumn::DateTime", "TimeStamp");
=head1 NAME
is_auto_increment: 1
is_nullable: 0
+=head2 email
+
+ data_type: 'text'
+ is_nullable: 1
+
=head2 username
data_type: 'text'
=head2 password
data_type: 'text'
- is_nullable: 1
+ is_nullable: 0
=head2 status
__PACKAGE__->add_columns(
"id",
{ data_type => "integer", is_auto_increment => 1, is_nullable => 0 },
+ "email",
+ { data_type => "text", is_nullable => 1 },
"username",
{ data_type => "text", is_nullable => 1 },
"password",
- { data_type => "text", is_nullable => 1 },
+ { data_type => "text", is_nullable => 0 },
"status",
{ data_type => "text", default_value => "active", is_nullable => 1 },
);
__PACKAGE__->set_primary_key("id");
+__PACKAGE__->add_unique_constraint("username_unique", ["username"]);
=head1 RELATIONS
);
-# Created by DBIx::Class::Schema::Loader v0.07010 @ 2011-10-12 22:20:29
-# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:D8HUHJmSfJwylSeDYjfeHA
+# Created by DBIx::Class::Schema::Loader v0.07010 @ 2011-10-13 16:46:39
+# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:pPJdUbHgHvUo4FxblDaJ2g
# You can replace this text with custom code or comments, and it will be preserved on regeneration
use namespace::autoclean;
extends 'DBIx::Class::Core';
-__PACKAGE__->load_components("InflateColumn::DateTime");
+__PACKAGE__->load_components("InflateColumn::DateTime", "TimeStamp");
=head1 NAME
);
-# Created by DBIx::Class::Schema::Loader v0.07010 @ 2011-10-12 22:20:29
-# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:dORhf3WubIeixtSujgUgrg
+# Created by DBIx::Class::Schema::Loader v0.07010 @ 2011-10-13 16:46:39
+# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:dgMXiWuIhmCQeExkpcxorA
# You can replace this text with custom code or comments, and it will be preserved on regeneration
}
label {
- width: 100px;
+ width: 150px;
float: left;
}
#outer {
margin: 0 auto 0 auto;
- width: 640px;
+ width: 720px;
border: 2px solid black;
background: #99c;
}
/* error message */
.error {
- /* supply height to ensure consistent positioning for every browser */
- height:15px;
- background-color:#FFFE36;
- border:1px solid #E1E16D;
- font-size:11px;
- color:#000;
- padding:3px 10px;
- margin-left:-2px;
-
-
- /* CSS3 spicing for mozilla and webkit */
- -moz-border-radius:4px;
- -webkit-border-radius:4px;
- -moz-border-radius-bottomleft:0;
- -moz-border-radius-topleft:0;
- -webkit-border-bottom-left-radius:0;
- -webkit-border-top-left-radius:0;
-
- -moz-box-shadow:0 0 6px #ddd;
- -webkit-box-shadow:0 0 6px #ddd;
+ background: #c66;
}
[% META title = 'Log In' -%]
<h2>Log In</h2>
[% IF ! c.user_exists -%]
+<!--
<form action="[% c.uri_for('login') %]" method="post">
<fieldset>
<p>
<input type="submit" value="Login">
</fieldset>
</form>
-<h3>Don't have an account?</h3>
+-->
+[% form.render %]
+<h3>Register</h3>
<p>
-If you haven't yet registered, <a href="[% c.uri_for('register') %]">go do that now</a>!
+If you don't already have an account, <a href="[% c.uri_for('register') %]">go register</a>!
</p>
[% ELSE -%]
<p>You are already logged in.</p>
[% END -%]
<h2>Register</h2>
[% IF ! c.user_exists -%]
+<!--
<form id="form" action="[% c.uri_for('register') %]" method="post">
<fieldset>
<p>
<button type="reset">Reset</button>
</fieldset>
</form>
+-->
+[% form.render %]
[% ELSE -%]
<p>You are already registered and logged in. There is no need to register again.</p>
[% END -%]