]>
Dogcows Code - chaz/chatty/blob - lib/Chatty/Controller/Root.pm
1 package Chatty
::Controller
::Root
;
3 use namespace
::autoclean
;
5 BEGIN { extends
'Catalyst::Controller' }
8 # Sets the actions in this controller to be registered with no prefix
9 # so they function identically to actions created in MyApp.pm
11 __PACKAGE__-
>config(namespace
=> '');
13 use JSON
'encode_json';
15 use Chatty
::Form
::Login
;
16 use Chatty
::Form
::Register
;
19 isa
=> 'Chatty::Form::Login',
22 default => sub { Chatty
::Form
::Login-
>new }
25 has 'register_form' => (
26 isa
=> 'Chatty::Form::Register',
29 default => sub { Chatty
::Form
::Register-
>new }
34 Chatty::Controller::Root - Root Controller for Chatty
38 Implements all actions for this simple chat application.
48 sub index :Path
:Args
(0) {
50 $c->go('/chat/list') if ($c->user_exists);
56 Allow a user to login.
60 sub login
:Local
:Args
(0) {
63 $c->stash(form
=> $self->login_form);
64 $self->login_form->process($c->req->params);
65 return unless $self->login_form->is_valid;
68 if ($c->authenticate({
69 username
=> $self->login_form->value->{username
},
70 password
=> $self->login_form->value->{password
}
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'));
79 $c->flash->{error
} = "Log-in failed! Try again, I guess.";
80 $c->res->redirect($c->uri_for_action('login'));
91 sub logout
:Local
:Args
(0) {
93 if ($c->user_exists) {
95 $c->flash->{message
} = "Goodbye! You have been logged out.";
97 $c->res->redirect($c->uri_for_action('index'));
102 Register a new account.
106 sub register
:Local
:Args
(0) {
109 $c->stash(form
=> $self->register_form);
111 my $new_account = $c->model('DB::Account')->new_result({});
112 $self->register_form->process(
113 item
=> $new_account,
114 params
=> $c->req->params
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...";
124 $c->flash->{message
} = "Registration complete. ";
125 $c->forward('login');
128 =head2 register_validate
130 Check whether or not a username is available.
134 sub register_validate
:Local
:Args
(0) {
137 my $id = $c->req->param('fieldId');
138 my $username = $c->req->param('fieldValue');
143 my $account = $c->model('DB::Account')->find({username
=> $username});
145 $json_arr = ["$id", 1, "This username is available. Nice!"];
148 $json_arr = ["$id", 0, "This username is taken."];
152 $json_arr = ["$id", 0, "Invalid arguments to check script."];
154 $c->res->content_type("application/json");
155 $c->res->body(encode_json
($json_arr));
160 Standard 403 error page
164 sub access_denied
:Private
{
166 $c->res->body('Access denied.');
167 $c->res->status(403);
172 Standard 404 error page
176 sub missing
:Private
{
178 $c->res->body('Page not found.');
179 $c->res->status(404);
184 Standard 404 error page
190 $c->detach('missing');
195 Attempt to render a view, if needed.
199 sub end
: ActionClass
('RenderView') {}
207 This library is free software. You can redistribute it and/or modify
208 it under the same terms as Perl itself.
212 __PACKAGE__-
>meta->make_immutable;
This page took 0.046206 seconds and 5 git commands to generate.