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';
ConfigLoader
StackTrace
Static::Simple
+ Authentication
+ Session
+ Session::Store::FastMmap
+ Session::State::Cookie
/;
extends 'Catalyst';
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();
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
=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);
}
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;
+}
+
--- /dev/null
+<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 %]
<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 © 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>