From 10a4a6d0a5e2af19145d73956c28a7dfa8d98289 Mon Sep 17 00:00:00 2001 From: Gryllida A Date: Sun, 24 Feb 2013 17:46:26 +1030 Subject: [PATCH] An attempt at adding registration --- lib/Slists/Controller/Login.pm | 75 +++++++++++++++++++++++++++++++----------- root/src/login.tt2 | 4 +++ 2 files changed, 59 insertions(+), 20 deletions(-) diff --git a/lib/Slists/Controller/Login.pm b/lib/Slists/Controller/Login.pm index c06dc44..a6d94f6 100644 --- a/lib/Slists/Controller/Login.pm +++ b/lib/Slists/Controller/Login.pm @@ -1,6 +1,7 @@ package Slists::Controller::Login; use Moose; use namespace::autoclean; +use Slists::Form::User; BEGIN { extends 'Catalyst::Controller'; } @@ -25,35 +26,69 @@ Login logic sub index :Path :Args(0) { my ($self, $c) = @_; - - # Get the username and password from form - my $username = $c->request->params->{username}; - my $password = $c->request->params->{password}; - - # If the username and password values were found in form - if ($username && $password) { - # Attempt to log the user in - if ($c->authenticate({ username => $username, - password => $password } )) { - # If successful, then let them use the application - $c->response->redirect($c->uri_for( - $c->controller('Lists')->action_for('list'))); - return; + + if ($c->request->method eq "POST"){ + # Get the username and password from form + my $username = $c->request->params->{username}; + my $password = $c->request->params->{password}; + + # If the username and password values were found in form + if ($username && $password) { + # Attempt to log the user in + if ($c->authenticate({ username => $username, + password => $password } )) { + # If successful, then let them use the application + $c->response->redirect($c->uri_for( + $c->controller('Lists')->action_for('list'))); + return; + } else { + # Set an error message + $c->stash(error_msg => "Bad username or password."); + } } else { # Set an error message - $c->stash(error_msg => "Bad username or password."); + $c->stash(error_msg => "Empty username or password.") + unless ($c->user_exists); } - } else { - # Set an error message - $c->stash(error_msg => "Empty username or password.") - unless ($c->user_exists); } - # If either of above don't work out, send to the login page $c->stash(template => 'login.tt2'); } +=head2 create + +Use HTML::FormHandler to create a new list + +=cut + +sub register :Path('/register') :Args(0) { + my ($self, $c ) = @_; + my $user = $c->model('DB::User')->new_result({}); + $c->stash( my_status_msg => "Registration successful" ); + return $self->form($c, $user); +} + +=head2 form + +Process the FormHandler list form +This is a separate function, because it's used by both /create and /edit. +=cut + +sub form{ + my ( $self, $c, $user ) = @_; + my $form = Slists::Form::User->new; + # Set the template + $c->stash( template => 'user.tt2', form => $form ); + $form->process( + item => $user, + params => $c->req->params, + ); + return unless $form->validated; + # Set a status message for the user & return to books list + return $c->response->redirect($c->uri_for($self->action_for('login'), + {mid => $c->set_status_msg($c->stash->{my_status_msg})})); +} =head1 AUTHOR diff --git a/root/src/login.tt2 b/root/src/login.tt2 index ca471fe..e8153c7 100644 --- a/root/src/login.tt2 +++ b/root/src/login.tt2 @@ -17,3 +17,7 @@ +

Not registered? +Register +

+ -- 2.11.4.GIT