Implement login page

This commit is contained in:
swagg boi 2022-11-19 23:55:03 -05:00
parent a9b8616f2d
commit 4a59dd69dc
7 changed files with 81 additions and 1 deletions

View file

@ -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;

View 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;

View file

@ -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,

View file

@ -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>
<% } =%>

View file

@ -0,0 +1,4 @@
% layout 'main';
% title 'Top Secret';
<h2><%= title %></h2>
<p>For mods only!!</p>

View file

@ -0,0 +1,4 @@
% layout 'main';
% title 'Top Secret';
<h2><%= title %></h2>
<p>For mods only!!</p>

View 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>