Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Listy::Controller::User;
- use strict;
- use warnings;
- use Captcha::reCAPTCHA;
- use base 'Catalyst::Controller::reCAPTCHA';
- =head1 NAME
- Listy::Controller::User - Catalyst Controller
- =head1 DESCRIPTION
- Catalyst Controller.
- =head1 METHODS
- =cut
- =head2 index
- =cut
- sub index : Private {
- my ( $self, $c ) = @_;
- $c->response->body('Matched Listy::Controller::User in User.');
- }
- sub signup :Local {
- my ($self, $c) = @_;
- $c->forward('captcha_get');
- if ($c->request->method eq 'GET') {
- $c->stash->{template} = 'user/signup.tt2';
- return;
- }
- my $error_msgs = {};
- my $username = lc ($c->request->params->{username} || "");
- my $password = $c->request->params->{password} || "";
- my $confirm_password = $c->request->params->{confirm_password} || "";
- if (length $password < 8) {
- $error_msgs -> {password} = "Must be at least 8 characters";
- }
- if ($password ne $confirm_password) {
- $error_msgs -> {confirm_password} = "Did not match password";
- }
- if (not $username) {
- $error_msgs -> {username} = "Must not be blank";
- } elsif ($c->model('DB::Users')->find({username => $username})) {
- $error_msgs -> {username} = "Username '$username' taken";
- }
- $c->forward('captcha_check');
- if (not $c->stash->{recaptcha_ok}) {
- $error_msgs -> {recaptcha} = $c->stash->{recaptcha_error}; #"CAPTCHA attempt failed";
- }
- my $successful = scalar keys %$error_msgs;
- if ($successful) {
- my $user = $c -> model('DB::Users') ->
- create({username => $username, password => $password });
- if (not $user) {
- $successful = 0;
- $error_msgs -> {general} = "There was a system failure. Please try again later."
- }
- }
- if ($successful) {
- $c->forward('login')
- } else {
- $c->stash->{error_msgs} = $error_msgs;
- $c->stash->{username} = $username;
- $c->stash->{template} = 'user/signup.tt2';
- }
- }
- sub login :Local {
- my ($self, $c) = @_;
- # Get the username and password from form
- my $username = $c->request->params->{username} || "";
- my $password = $c->request->params->{password} || "";
- my $next_page = $c->request->params->{next_page} || "/lists/";
- # If the username and password values were found in form
- if ($username && $password) {
- # Attempt to log the user in
- if ($c->authenticate({ username => $username,
- password => $password} )) {
- # If successful, then let them use the application
- $c->response->redirect($c->uri_for($next_page));
- return;
- } else {
- # Set an error message
- $c->stash->{error_msg} = "Bad username or password.";
- }
- }
- # If either of above don't work out, send to the login page
- $c->stash->{template} = 'login.tt2';
- }
- sub logout :Local {
- my ($self, $c) = @_;
- # Clear the user's state
- $c->logout;
- #Send the user to the starting point
- $c->response->redirect($c->uri_for('/'));
- }
- =head1 AUTHOR
- ,,,
- =head1 LICENSE
- This library is free software, you can redistribute it and/or modify
- it under the same terms as Perl itself.
- =cut
- 1;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement