return from eval doesn't return function
[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 JSON 'encode_json';
14
15 use Chatty::Form::Login;
16 use Chatty::Form::Register;
17
18 has 'login_form' => (
19 isa => 'Chatty::Form::Login',
20 is => 'rw',
21 lazy => 1,
22 default => sub { Chatty::Form::Login->new }
23 );
24
25 has 'register_form' => (
26 isa => 'Chatty::Form::Register',
27 is => 'rw',
28 lazy => 1,
29 default => sub { Chatty::Form::Register->new }
30 );
31
32 =head1 NAME
33
34 Chatty::Controller::Root - Root Controller for Chatty
35
36 =head1 DESCRIPTION
37
38 Implements all actions for this simple chat application.
39
40 =head1 METHODS
41
42 =head2 index
43
44 The root page (/)
45
46 =cut
47
48 sub index :Path :Args(0) {
49 my ($self, $c) = @_;
50 $c->go('/chat/list') if ($c->user_exists);
51 $c->go('login');
52 }
53
54 =head2 login
55
56 Allow a user to login.
57
58 =cut
59
60 sub login :Local :Args(0) {
61 my ($self, $c) = @_;
62
63 $c->stash(form => $self->login_form);
64 $self->login_form->process($c->req->params);
65 return unless $self->login_form->is_valid;
66
67 eval {
68 if ($c->authenticate({
69 username => $self->login_form->value->{username},
70 password => $self->login_form->value->{password}
71 })) {
72 $c->change_session_id;
73 my $user = $c->user->get('username');
74 $c->flash->{message} .= "Hi, $user! You are now logged in.";
75 $c->res->redirect($c->uri_for_action('index'));
76 return;
77 }
78 else {
79 $c->flash->{error} = "Log-in failed! Try again, I guess.";
80 $c->res->redirect($c->uri_for_action('login'));
81 }
82 };
83 }
84
85 =head2 logout
86
87 Log the user out.
88
89 =cut
90
91 sub logout :Local :Args(0) {
92 my ($self, $c) = @_;
93 if ($c->user_exists) {
94 $c->logout;
95 $c->flash->{message} = "Goodbye! You have been logged out.";
96 }
97 $c->res->redirect($c->uri_for_action('index'));
98 }
99
100 =head2 register
101
102 Register a new account.
103
104 =cut
105
106 sub register :Local :Args(0) {
107 my ($self, $c) = @_;
108
109 $c->stash(form => $self->register_form);
110
111 my $new_account = $c->model('DB::Account')->new_result({});
112 $self->register_form->process(
113 item => $new_account,
114 params => $c->req->params
115 );
116
117 if (!$self->register_form->is_valid) {
118 if ($c->req->method eq 'POST') {
119 $c->stash->{error} = "The form has a validation error. Try again...";
120 }
121 return;
122 }
123
124 $c->flash->{message} = "Registration complete. ";
125 $c->forward('login');
126 }
127
128 =head2 register_validate
129
130 Check whether or not a username is available.
131
132 =cut
133
134 sub register_validate :Local :Args(0) {
135 my ($self, $c) = @_;
136
137 my $id = $c->req->param('fieldId');
138 my $username = $c->req->param('fieldValue');
139
140 my $json_arr = [];
141
142 if ($username) {
143 my $account = $c->model('DB::Account')->find({username => $username});
144 if (!$account) {
145 $json_arr = ["$id", 1, "This username is available. Nice!"];
146 }
147 else {
148 $json_arr = ["$id", 0, "This username is taken."];
149 }
150 }
151 else {
152 $json_arr = ["$id", 0, "Invalid arguments to check script."];
153 }
154 $c->res->content_type("application/json");
155 $c->res->body(encode_json($json_arr));
156 }
157
158 =head2 access_denied
159
160 Standard 403 error page
161
162 =cut
163
164 sub access_denied :Private {
165 my ($self, $c) = @_;
166 $c->res->body('Access denied.');
167 $c->res->status(403);
168 }
169
170 =head2 missing
171
172 Standard 404 error page
173
174 =cut
175
176 sub missing :Private {
177 my ($self, $c) = @_;
178 $c->res->body('Page not found.');
179 $c->res->status(404);
180 }
181
182 =head2 default
183
184 Standard 404 error page
185
186 =cut
187
188 sub default :Path {
189 my ($self, $c) = @_;
190 $c->detach('missing');
191 }
192
193 =head2 end
194
195 Attempt to render a view, if needed.
196
197 =cut
198
199 sub end : ActionClass('RenderView') {}
200
201 =head1 AUTHOR
202
203 Charles McGarvey
204
205 =head1 LICENSE
206
207 This library is free software. You can redistribute it and/or modify
208 it under the same terms as Perl itself.
209
210 =cut
211
212 __PACKAGE__->meta->make_immutable;
213
214 1;
This page took 0.042291 seconds and 4 git commands to generate.