Refactor login.

master
Manager Bot 3 years ago
parent 3b9cb01f22
commit d30d05e4c0
  1. 13
      Web/lib/MJB/Web.pm
  2. 43
      Web/lib/MJB/Web/Controller/Auth.pm
  3. 4
      Web/templates/auth/login.html.ep

@ -63,6 +63,15 @@ sub startup ($self) {
$blog->create_related( 'jobs', { minion_job_id => $build_job_id } );
});
# Helper to redirect on errors, support setting the form and errors in a flash
# if they exist in the stash.
$self->helper( redirect_error => sub ( $c, $redirect_to ) {
$c->flash( form => $c->stash->{form} ) if $c->stash->{form};
$c->flash( errors => $c->stash->{errors} ) if $c->stash->{errors};
$c->redirect_to( $c->url_for( $redirect_to ) );
});
# Minion plugin & tasks
$self->plugin( Minion => { Pg => $self->config->{database}->{minion} } );
@ -88,6 +97,10 @@ sub startup ($self) {
}
}
# If the user filled a form out and there was an error, we may have
# the content of the form in a flash, let's load that into the stash.
$c->stash->{form} = $c->flash( 'form' );
return 1;
});

@ -67,32 +67,57 @@ sub do_register ( $c ) {
}
sub login ( $c ) {
if ( $c->stash->{person} ) {
$c->redirect_to( $c->url_for( 'show_dashboard' ) );
}
}
#==
# POST /login | do_login
# email - The email address of the account to login to.
# password - The password for the account to login to.
#
# Try to login to the account owned by the email address with the
# supplied password.
#
# If the account exists and password matches, set the session uid
# to the user's account id. This will load the correct account to
# $c->stash->{person} on the next page load.
#
# Show the login page with error messages when there has been an error.
#
# Redirect the user to the dashboard on successful login.
#==
sub do_login ( $c ) {
$c->stash->{template} = 'auth/login';
my $email = $c->stash->{form}->{email} = $c->param('email');
my $password = $c->stash->{form}->{password} = $c->param('password');
# Did we get an email address and a password?
push @{$c->stash->{errors}}, "You must supply an email address to login."
unless $email;
my $email = $c->stash->{form_email} = $c->param('email');
my $password = $c->stash->{form_password} = $c->param('password');
push @{$c->stash->{errors}}, "You must suply a password to login."
unless $password;
return $c->redirect_error( 'show_login' )
if $c->stash->{errors};
# Can we load a user account?
my $person = $c->db->resultset('Person')->find( { email => $email } )
or push @{$c->stash->{errors}}, "Invalid email address or password.";
return 0 if $c->stash->{errors};
return $c->redirect_error( 'show_login' )
if $c->stash->{errors};
# Does the user account we loaded have a password that matches the one supplied?
$person->auth_password->check_password( $password )
or push @{$c->stash->{errors}}, "Invalid email address or password.";
return 0 if $c->stash->{errors};
$c->stash->{person} = $person;
return $c->redirect_error( 'show_login' )
if $c->stash->{errors};
# Everything is good, log the user in and send them to the dashboard.
$c->session->{uid} = $person->id;
$c->redirect_to( $c->url_for( 'show_dashboard' ) );
}

@ -9,13 +9,13 @@
<%= include '_base/form/input', type => 'email', name => 'email',
title => 'Email Address',
help => '',
value => $c->stash->{form_email}
value => $c->stash->{form}->{email}
%>
<%= include '_base/form/input', type => 'password', name => 'password',
title => 'Password',
help => '',
value => $c->stash->{form_password}
value => $c->stash->{form}->{password}
%>
<button type="submit" class="btn btn-primary float-end">Login</button>

Loading…
Cancel
Save