]> Dogcows Code - chaz/chatty/blob - lib/Chatty/Controller/Root.pm
switch to FormHandler for validation
[chaz/chatty] / lib / Chatty / Controller / Root.pm
1 package Chatty::Controller::Root;
2 use Moose;
3 use namespace::autoclean;
4
5 BEGIN { extends 'Catalyst::Controller' }
6
7 #
8 # Sets the actions in this controller to be registered with no prefix
9 # so they function identically to actions created in MyApp.pm
10 #
11 __PACKAGE__->config(namespace => '');
12
13 use Chatty::Form::Login;
14 use Chatty::Form::Register;
15
16 has 'login_form' => (
17 isa => 'Chatty::Form::Login',
18 is => 'rw',
19 lazy => 1,
20 default => sub { Chatty::Form::Login->new }
21 );
22
23 has 'register_form' => (
24 isa => 'Chatty::Form::Register',
25 is => 'rw',
26 lazy => 1,
27 default => sub { Chatty::Form::Register->new }
28 );
29
30 =head1 NAME
31
32 Chatty::Controller::Root - Root Controller for Chatty
33
34 =head1 DESCRIPTION
35
36 Implements all actions for this simple chat application.
37
38 =head1 METHODS
39
40 =head2 index
41
42 The root page (/)
43
44 =cut
45
46 sub index :Path :Args(0) {
47 my ( $self, $c ) = @_;
48 }
49
50 =head2 login
51
52 Allow a user to login.
53
54 =cut
55
56 sub login :Local :Args(0) {
57 my ($self, $c) = @_;
58
59 $c->stash(form => $self->login_form);
60 $self->login_form->process($c->req->params);
61 return unless $self->login_form->is_valid;
62
63 eval {
64 if ($c->authenticate({
65 username => $self->login_form->value->{username},
66 password => $self->login_form->value->{password}
67 })) {
68 $c->change_session_id;
69 my $user = $c->user->get('username');
70 $c->flash->{message} .= "Hi, $user! You are now logged in.";
71 $c->response->redirect($c->uri_for('/'));
72 }
73 else {
74 $c->flash->{error} = "Log-in failed! Try again, I guess.";
75 $c->response->redirect($c->uri_for('login'));
76 }
77 }
78 }
79
80 =head2 logout
81
82 Log the user out.
83
84 =cut
85
86 sub logout :Local :Args(0) {
87 my ($self, $c) = @_;
88 if ($c->user_exists) {
89 $c->logout;
90 $c->flash->{message} = "Goodbye! You have been logged out.";
91 }
92 $c->response->redirect($c->uri_for('/'));
93 }
94
95 =head2 register
96
97 Register a new account.
98
99 =cut
100
101 sub register :Local :Args(0) {
102 my ($self, $c) = @_;
103
104 $c->stash(form => $self->register_form);
105
106 my $new_account = $c->model('DB::Account')->new_result({});
107 $self->register_form->process(
108 item => $new_account,
109 params => $c->req->params
110 );
111
112 return unless $self->register_form->is_valid;
113
114 $c->flash->{message} = "Registration complete. ";
115 $c->forward('login');
116 }
117
118 =head2 default
119
120 Standard 404 error page
121
122 =cut
123
124 sub default :Path {
125 my ($self, $c) = @_;
126 $c->response->body('Page not found.');
127 $c->response->status(404);
128 }
129
130 =head2 end
131
132 Attempt to render a view, if needed.
133
134 =cut
135
136 sub end : ActionClass('RenderView') {}
137
138 =head1 AUTHOR
139
140 Charles McGarvey
141
142 =head1 LICENSE
143
144 This library is free software. You can redistribute it and/or modify
145 it under the same terms as Perl itself.
146
147 =cut
148
149 __PACKAGE__->meta->make_immutable;
150
151 1;
This page took 0.044824 seconds and 5 git commands to generate.