implemented session tracking and authentication
authorCharles McGarvey <chazmcgarvey@brokenzipper.com>
Thu, 13 Oct 2011 05:09:50 +0000 (23:09 -0600)
committerCharles McGarvey <chazmcgarvey@brokenzipper.com>
Thu, 13 Oct 2011 05:09:50 +0000 (23:09 -0600)
Makefile.PL
lib/Chatty.pm
lib/Chatty/Controller/Root.pm
root/static/css/common.css
root/tt/login.tt [new file with mode: 0644]
root/tt/wrapper.tt

index 765aa5a78b7dc8cd99629d90a6da78a439142021..18cbdfb39dbf515b80f827e95dc84f71aa94b814 100644 (file)
@@ -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';
index 0de57fd7182bb7a0a1a9d992224bc7510692fd71..72353a7ff8c1ce3e0397d13640a6dd4258472993 100644 (file)
@@ -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();
 
index 644a861a2d074191130835ef6296a4a3b4d1d7ac..f3e5030b2a82b34e825482da718b92d6eecc8f2a 100644 (file)
@@ -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);
 }
 
index 61d896a843e6d81df080f5d44695fed8cf3f5188..dec13c438a48bb275922f9d30eb449b19f4b78d7 100644 (file)
@@ -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 (file)
index 0000000..d2b70e2
--- /dev/null
@@ -0,0 +1,13 @@
+<h1>Log In</h1>
+[% IF ! c.user_exists %]
+<form action="[% c.uri_for('login')  %]" method="post">
+       <label for="handle">Username</label>
+       <input type="text" name="handle" id="handle">
+       <label for="password">Password</label>
+       <input type="password" name="password" id="password">
+       <input type="submit" value="Login">
+</form>
+[% ELSE %]
+<p>You are already logged in.</p>
+<a href="[% c.uri_for('logout') %]">Log Out</a>
+[% END %]
index 951a19e4fb007c5bb161ff8e7f595c7e81736687..7599375496479d2ef5ef3f76523a27faca3f2e14 100644 (file)
@@ -4,11 +4,17 @@
                <meta charset="utf-8">
                <meta name="author" content="Charles McGarvey">
                <meta name="description" content="This is a chat application in Catalyst!">
-               <link rel="stylesheet" type="text/css" href="[% c.uri_for('static/css/common.css') %]">
+               <link rel="stylesheet" type="text/css" href="[% c.uri_for('/static/css/common.css') %]">
                <title>Chatty - [% template.title or 'Toy chat application written in Perl/Catalyst' %]</title>
        </head>
        <body>
                <div id="outer">
+[% IF error -%]
+                               <p id="error">[% error %]</p>
+[% END -%]
+[% IF message -%]
+                               <p id="message">[% message %]</p>
+[% END -%]
                        <div id="inner">
 [% content %]
                                <hr>
                                        Copyright &copy; 2011 Charles McGarvey. Some rights reserved.
                                        <div class="right">
                                                <a href="http://www.catalystframework.org/">
-                                                       <img src="[% c.uri_for('static/img/btn_88x31_built_shadow.png') %]">
+                                                       <img src="[% c.uri_for('/static/img/btn_88x31_built_shadow.png') %]">
                                                </a>
                                        </div>
                                </div>
                        </div>
                </div>
-               <script type="text/javascript" src="[% static('js/jquery.tools-1.2.6.min.js') %]"></script>
+               <script type="text/javascript" src="[% c.uri_for('/static/js/jquery.tools-1.2.6.min.js') %]"></script>
+               <script type="text/javascript">
+[% IF error -%]
+                       $(function () {
+                                       $("#error").expose({color: '#999'}).click(function() {
+                                                       $.mask.close();
+                                                       $(this).remove();
+                                               });
+                       });
+[% END -%]
+               </script>
        </body>
 </html>
This page took 0.032503 seconds and 4 git commands to generate.