From 9b343fd3d0f46473e2f5804ac09caeaed95e4283 Mon Sep 17 00:00:00 2001 From: Charles McGarvey Date: Wed, 12 Oct 2011 23:09:50 -0600 Subject: [PATCH] implemented session tracking and authentication --- Makefile.PL | 4 +++ lib/Chatty.pm | 17 +++++++++++++ lib/Chatty/Controller/Root.pm | 46 +++++++++++++++++++++++++++++++++-- root/static/css/common.css | 16 ++++++++++++ root/tt/login.tt | 13 ++++++++++ root/tt/wrapper.tt | 22 ++++++++++++++--- 6 files changed, 113 insertions(+), 5 deletions(-) create mode 100644 root/tt/login.tt diff --git a/Makefile.PL b/Makefile.PL index 765aa5a..18cbdfb 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -13,6 +13,10 @@ requires 'Catalyst::Runtime' => '5.90002'; requires 'Catalyst::Plugin::ConfigLoader'; requires 'Catalyst::Plugin::StackTrace'; requires 'Catalyst::Plugin::Static::Simple'; +requires 'Catalyst::Plugin::Authentication'; +requires 'Catalyst::Plugin::Session'; +requires 'Catalyst::Plugin::Session::Store::FastMmap'; +requires 'Catalyst::Plugin::Session::State::Cookie'; requires 'Catalyst::Action::RenderView'; requires 'Moose'; requires 'namespace::autoclean'; diff --git a/lib/Chatty.pm b/lib/Chatty.pm index 0de57fd..72353a7 100644 --- a/lib/Chatty.pm +++ b/lib/Chatty.pm @@ -21,6 +21,10 @@ use Catalyst qw/ ConfigLoader StackTrace Static::Simple + Authentication + Session + Session::Store::FastMmap + Session::State::Cookie /; extends 'Catalyst'; @@ -42,6 +46,19 @@ __PACKAGE__->config( disable_component_resolution_regex_fallback => 1, ); +__PACKAGE__->config( + 'Plugin::Authentication' => { + default => { + class => 'SimpleDB', + user_model => 'DB::Account', + password_type => 'clear', + }, + }, + 'Plugin::Session' => { + flash_to_stash => 1 + } +); + # Start the application __PACKAGE__->setup(); diff --git a/lib/Chatty/Controller/Root.pm b/lib/Chatty/Controller/Root.pm index 644a861..f3e5030 100644 --- a/lib/Chatty/Controller/Root.pm +++ b/lib/Chatty/Controller/Root.pm @@ -30,6 +30,48 @@ sub index :Path :Args(0) { my ( $self, $c ) = @_; } +=head2 login + +Allow a user to login. + +=cut + +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')); + } + } + } +} + +=head2 logout + +Log the user out. + +=cut + +sub logout :Local :Args(0) { + my ($self, $c) = @_; + if ($c->user_exists) { + $c->logout; + $c->flash->{message} = "Goodbye! You have been logged out."; + } + $c->response->redirect($c->uri_for('/')); +} + =head2 default Standard 404 error page @@ -37,8 +79,8 @@ Standard 404 error page =cut sub default :Path { - my ( $self, $c ) = @_; - $c->response->body( 'Page not found' ); + my ($self, $c) = @_; + $c->response->body('Page not found.'); $c->response->status(404); } diff --git a/root/static/css/common.css b/root/static/css/common.css index 61d896a..dec13c4 100644 --- a/root/static/css/common.css +++ b/root/static/css/common.css @@ -59,3 +59,19 @@ a:hover { font-size: 0.8em; } +#error { + margin: 5px; + padding: 10px; + border: 2px solid black; + background: white; + color: red; + font-weight: bold; +} + +#message { + margin: 5px; + padding: 10px; + border: 2px solid black; + background: white; +} + diff --git a/root/tt/login.tt b/root/tt/login.tt new file mode 100644 index 0000000..d2b70e2 --- /dev/null +++ b/root/tt/login.tt @@ -0,0 +1,13 @@ +

Log In

+[% IF ! c.user_exists %] +
+ + + + + +
+[% ELSE %] +

You are already logged in.

+Log Out +[% END %] diff --git a/root/tt/wrapper.tt b/root/tt/wrapper.tt index 951a19e..7599375 100644 --- a/root/tt/wrapper.tt +++ b/root/tt/wrapper.tt @@ -4,11 +4,17 @@ - + Chatty - [% template.title or 'Toy chat application written in Perl/Catalyst' %]
+[% IF error -%] +

[% error %]

+[% END -%] +[% IF message -%] +

[% message %]

+[% END -%]
[% content %]
@@ -16,12 +22,22 @@ Copyright © 2011 Charles McGarvey. Some rights reserved.
- + + -- 2.45.2