Add link to mod login; change the way session is stored; add route to logout; some clean-up

This commit is contained in:
swag 2022-11-21 21:59:43 -05:00
parent 9089b78db7
commit 87dc291c86
5 changed files with 58 additions and 17 deletions

View File

@ -83,7 +83,7 @@ sub startup($self) {
my $thread = $r->under('/thread'); my $thread = $r->under('/thread');
$thread->under('/list') $thread->under('/list')
->get('/:list_page', [list_page => qr/[0-9]+/], {list_page => 1}) ->get('/:list_page', [list_page => qr/\d+/], {list_page => 1})
->to('thread#by_page') ->to('thread#by_page')
->name('threads_list'); ->name('threads_list');
@ -91,18 +91,18 @@ sub startup($self) {
->to('thread#create') ->to('thread#create')
->name('post_thread'); ->name('post_thread');
$thread->under('/single/:thread_id', [thread_id => qr/[0-9]+/]) $thread->under('/single/:thread_id', [thread_id => qr/\d+/])
->get('/:thread_page', [thread_page => qr/[0-9]+/], {thread_page => 1}) ->get('/:thread_page', [thread_page => qr/\d+/], {thread_page => 1})
->to('thread#by_id') ->to('thread#by_id')
->name('single_thread'); ->name('single_thread');
$thread->under('/bump') $thread->under('/bump')
->get('/:thread_id', [thread_id => qr/[0-9]+/]) ->get('/:thread_id', [thread_id => qr/\d+/])
->to('thread#bump') ->to('thread#bump')
->name('bump_thread'); ->name('bump_thread');
$thread->under('/flag') $thread->under('/flag')
->get('/:thread_id', [thread_id => qr/[0-9]+/]) ->get('/:thread_id', [thread_id => qr/\d+/])
->to('thread#flag') ->to('thread#flag')
->name('flag_thread'); ->name('flag_thread');
@ -110,35 +110,41 @@ sub startup($self) {
my $remark = $r->under('/remark'); my $remark = $r->under('/remark');
$remark->under('/post') $remark->under('/post')
->any([qw{GET POST}], '/:thread_id', [thread_id => qr/[0-9]+/]) ->any([qw{GET POST}], '/:thread_id', [thread_id => qr/\d+/])
->to('remark#create') ->to('remark#create')
->name('post_remark'); ->name('post_remark');
$remark->under('/single') $remark->under('/single')
->get('/:remark_id', [remark_id => qr/[0-9]+/]) ->get('/:remark_id', [remark_id => qr/\d+/])
->to('remark#by_id') ->to('remark#by_id')
->name('single_remark'); ->name('single_remark');
$remark->under('/flag') $remark->under('/flag')
->get('/:remark_id', [remark_id => qr/[0-9]+/]) ->get('/:remark_id', [remark_id => qr/\d+/])
->to('remark#flag') ->to('remark#flag')
->name('flag_remark'); ->name('flag_remark');
# Login # Login/out
$r->any([qw{GET POST}], '/login') $r->any([qw{GET POST}], '/login')
->to('moderator#login') ->to('moderator#login')
->name('mod_login'); ->name('mod_login');
$r->get('/logout')
->to('moderator#logout')
->name('mod_logout');
# Moderator # Moderator
my $moderator = $r->under('/moderator', sub ($c) { my $moderator = $r->under('/moderator', sub ($c) {
return 1 if $c->session('moderator'); return 1 if $c->session('mod_id') =~ /^\d+$/;
$c->redirect_to('mod_login'); $c->redirect_to('mod_login');
# Return false otherwise a body is rendered with the redirect...
return undef; return undef;
}); });
$moderator->get('/list')->to('moderator#list')->name('mod_list'); $moderator->get('/list')
->to('moderator#list')
->name('mod_list');
} }
1; 1;

View File

@ -7,10 +7,14 @@ sub list($self) { $self->render }
sub login($self) { sub login($self) {
my $v; my $v;
#Already logged in?
return $self->redirect_to('mod_list')
if $self->session('mod_id') =~ /^\d$/;
$v = $self->validation if $self->req->method eq 'POST'; $v = $self->validation if $self->req->method eq 'POST';
if ($v && $v->has_data) { if ($v && $v->has_data) {
my ($email, $password); my ($email, $password, $mod_id, $mod_name);
$v->required('email' ); $v->required('email' );
$v->required('password'); $v->required('password');
@ -22,12 +26,17 @@ sub login($self) {
$email = $self->param('email' ); $email = $self->param('email' );
$password = $self->param('password'); $password = $self->param('password');
$mod_id = $self->moderator->get_id($email);
$mod_name = $self->moderator->get_name($mod_id);
if ($self->moderator->check($email, $password)) { if ($self->moderator->check($email, $password)) {
$self->session(moderator => 1); $self->session(mod_id => $mod_id);
$self->flash(info => "Hello, $mod_name 😎");
return $self->redirect_to('mod_list'); return $self->redirect_to('mod_list');
} }
else { else {
$self->stash(status => 403);
$self->flash(error => 'Invalid login! 🧐') $self->flash(error => 'Invalid login! 🧐')
} }
} }
@ -36,4 +45,12 @@ sub login($self) {
$self->render; $self->render;
} }
sub logout($self) {
delete $self->session->{'mod_id'};
$self->flash(info => 'Logged out successfully 👋');
$self->redirect_to('threads_list');
}
1; 1;

View File

@ -7,7 +7,7 @@ has [qw{pg authenticator}];
sub check($self, $email, $password) { sub check($self, $email, $password) {
my $moderator = my $moderator =
$self->pg->db->query(<<~'END_SQL', $email)->hash; $self->pg->db->query(<<~'END_SQL', $email)->hash;
SELECT moderator_id AS id, SELECT moderator_id AS id,
password_hash password_hash
FROM moderators FROM moderators
WHERE email_addr = ?; WHERE email_addr = ?;
@ -19,4 +19,20 @@ sub check($self, $email, $password) {
->verify_password($password, $moderator->{'password_hash'}); ->verify_password($password, $moderator->{'password_hash'});
} }
sub get_id($self, $email) {
$self->pg->db->query(<<~'END_SQL', $email)->hash->{'moderator_id'}
SELECT moderator_id
FROM moderators
WHERE email_addr = ?;
END_SQL
}
sub get_name($self, $mod_id) {
$self->pg->db->query(<<~'END_SQL', $mod_id)->hash->{'moderator_name'}
SELECT moderator_name
FROM moderators
WHERE moderator_id = ?;
END_SQL
}
1; 1;

View File

@ -7,8 +7,9 @@
<body> <body>
<h1>Post::Text</h1> <h1>Post::Text</h1>
<nav> <nav>
<%= link_to List => 'threads_list' %> <%= link_to List => 'threads_list' %>
<%= link_to New => 'post_thread' %> <%= link_to New => 'post_thread' %>
<%= link_to Moderate => 'mod_list' %>
</nav> </nav>
<hr> <hr>
<% if (flash 'error') { =%> <% if (flash 'error') { =%>

View File

@ -2,3 +2,4 @@
% title 'Top Secret'; % title 'Top Secret';
<h2><%= title %></h2> <h2><%= title %></h2>
<p>For mods only!!</p> <p>For mods only!!</p>
<%= link_to Logout => 'mod_logout' %>