]>
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 Chatty
::Form
::Login
;
14 use Chatty
::Form
::Register
;
17 isa
=> 'Chatty::Form::Login',
20 default => sub { Chatty
::Form
::Login-
>new }
23 has 'register_form' => (
24 isa
=> 'Chatty::Form::Register',
27 default => sub { Chatty
::Form
::Register-
>new }
32 Chatty::Controller::Root - Root Controller for Chatty
36 Implements all actions for this simple chat application.
46 sub index :Path
:Args
(0) {
48 $c->go('/chat/list') if ($c->user_exists);
54 Allow a user to login.
58 sub login
:Local
:Args
(0) {
61 $c->stash(form
=> $self->login_form);
62 $self->login_form->process($c->req->params);
63 return unless $self->login_form->is_valid;
66 if ($c->authenticate({
67 username
=> $self->login_form->value->{username
},
68 password
=> $self->login_form->value->{password
}
70 $c->change_session_id;
71 my $user = $c->user->get('username');
72 $c->flash->{message
} .= "Hi, $user! You are now logged in.";
73 $c->res->redirect($c->uri_for_action('index'));
77 $c->flash->{error
} = "Log-in failed! Try again, I guess.";
78 $c->res->redirect($c->uri_for_action('login'));
89 sub logout
:Local
:Args
(0) {
91 if ($c->user_exists) {
93 $c->flash->{message
} = "Goodbye! You have been logged out.";
95 $c->res->redirect($c->uri_for_action('index'));
100 Register a new account.
104 sub register
:Local
:Args
(0) {
107 $c->stash(form
=> $self->register_form);
109 my $new_account = $c->model('DB::Account')->new_result({});
110 $self->register_form->process(
111 item
=> $new_account,
112 params
=> $c->req->params
115 if (!$self->register_form->is_valid) {
116 if ($c->req->method eq 'POST') {
117 $c->stash->{error
} = "The form has a validation error. Try again...";
122 $c->flash->{message
} = "Registration complete. ";
123 $c->forward('login');
126 =head2 register_validate
128 Check whether or not a username is available.
132 sub register_validate
:Local
:Args
(0) {
135 my $id = $c->req->param('fieldId');
136 my $username = $c->req->param('fieldValue');
139 my $account = $c->model('DB::Account')->find({username
=> $username});
141 $c->stash->{json
} = ["$id", 1, "This username is available. Nice!"];
144 $c->stash->{json
} = ["$id", 0, "This username is taken."];
148 $c->stash->{json
} = ["$id", 0, "Invalid arguments to check script."];
150 $c->forward('View::JSON');
155 Standard 403 error page
159 sub access_denied
:Private
{
161 $c->res->body('Access denied.');
162 $c->res->status(403);
167 Standard 404 error page
171 sub missing
:Private
{
173 $c->res->body('Page not found.');
174 $c->res->status(404);
179 Standard 404 error page
185 $c->detach('missing');
190 Attempt to render a view, if needed.
194 sub end
: ActionClass
('RenderView') {}
202 This library is free software. You can redistribute it and/or modify
203 it under the same terms as Perl itself.
207 __PACKAGE__-
>meta->make_immutable;
This page took 0.045571 seconds and 5 git commands to generate.