Implement login page
This commit is contained in:
parent
a9b8616f2d
commit
4a59dd69dc
|
@ -123,6 +123,22 @@ sub startup($self) {
|
|||
->get('/:remark_id', [remark_id => qr/[0-9]+/])
|
||||
->to('remark#flag')
|
||||
->name('flag_remark');
|
||||
|
||||
# Login
|
||||
$r->any([qw{GET POST}], '/login')
|
||||
->to('moderator#login')
|
||||
->name('mod_login');
|
||||
|
||||
# Moderator
|
||||
my $moderator = $r->under('/moderator', sub ($c) {
|
||||
return 1 if $c->session('moderator');
|
||||
|
||||
$c->redirect_to('mod_login');
|
||||
|
||||
return undef;
|
||||
});
|
||||
|
||||
$moderator->get('/list')->to('moderator#list')->name('mod_list');
|
||||
}
|
||||
|
||||
1;
|
||||
|
|
39
lib/PostText/Controller/Moderator.pm
Normal file
39
lib/PostText/Controller/Moderator.pm
Normal file
|
@ -0,0 +1,39 @@
|
|||
package PostText::Controller::Moderator;
|
||||
|
||||
use Mojo::Base 'Mojolicious::Controller', -signatures;
|
||||
|
||||
sub list($self) { $self->render }
|
||||
|
||||
sub login($self) {
|
||||
my $v;
|
||||
|
||||
$v = $self->validation if $self->req->method eq 'POST';
|
||||
|
||||
if ($v && $v->has_data) {
|
||||
my ($email, $password);
|
||||
|
||||
$v->required('email' );
|
||||
$v->required('password');
|
||||
|
||||
if ($v->has_error) {
|
||||
$self->stash(status => 400)
|
||||
}
|
||||
else {
|
||||
$email = $self->param('email' );
|
||||
$password = $self->param('password');
|
||||
|
||||
if ($self->moderator->check($email, $password)) {
|
||||
$self->session(moderator => 1);
|
||||
|
||||
return $self->redirect_to('mod_list');
|
||||
}
|
||||
else {
|
||||
$self->flash(error => 'Invalid login! 🧐')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$self->render;
|
||||
}
|
||||
|
||||
1;
|
|
@ -5,7 +5,7 @@ use Mojo::Base -base, -signatures;
|
|||
has 'pg';
|
||||
has 'authenticator';
|
||||
|
||||
sub check_password($self, $email, $password) {
|
||||
sub check($self, $email, $password) {
|
||||
my $moderator =
|
||||
$self->pg->db->query(<<~'END_SQL', $email)->hash;
|
||||
SELECT moderator_id AS id,
|
||||
|
|
|
@ -11,6 +11,9 @@
|
|||
<%= link_to New => 'post_thread' %>
|
||||
</nav>
|
||||
<hr>
|
||||
<% if (flash 'error') { =%>
|
||||
<p class="field-with-error" id="error"><%= flash 'error' %></p>
|
||||
<% } =%>
|
||||
<% if (flash 'info') { =%>
|
||||
<p class="field-with-info" id="info"><%= flash 'info' %></p>
|
||||
<% } =%>
|
||||
|
|
4
templates/moderator/index.html.ep
Normal file
4
templates/moderator/index.html.ep
Normal file
|
@ -0,0 +1,4 @@
|
|||
% layout 'main';
|
||||
% title 'Top Secret';
|
||||
<h2><%= title %></h2>
|
||||
<p>For mods only!!</p>
|
4
templates/moderator/list.html.ep
Normal file
4
templates/moderator/list.html.ep
Normal file
|
@ -0,0 +1,4 @@
|
|||
% layout 'main';
|
||||
% title 'Top Secret';
|
||||
<h2><%= title %></h2>
|
||||
<p>For mods only!!</p>
|
14
templates/moderator/login.html.ep
Normal file
14
templates/moderator/login.html.ep
Normal file
|
@ -0,0 +1,14 @@
|
|||
% layout 'main';
|
||||
% title 'Moderator Login';
|
||||
<h2><%= title %></h2>
|
||||
<form method="post">
|
||||
<div class="email field">
|
||||
<%= label_for email => 'Email' %>
|
||||
<%= email_field 'email' %>
|
||||
</div>
|
||||
<div class="password field">
|
||||
<%= label_for password => 'Password' %>
|
||||
<%= password_field 'password' %>
|
||||
</div>
|
||||
<%= submit_button 'Login' %>
|
||||
</form>
|
Loading…
Reference in a new issue