Add link to mod login; change the way session is stored; add route to logout; some clean-up
This commit is contained in:
parent
9089b78db7
commit
87dc291c86
|
@ -83,7 +83,7 @@ sub startup($self) {
|
|||
my $thread = $r->under('/thread');
|
||||
|
||||
$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')
|
||||
->name('threads_list');
|
||||
|
||||
|
@ -91,18 +91,18 @@ sub startup($self) {
|
|||
->to('thread#create')
|
||||
->name('post_thread');
|
||||
|
||||
$thread->under('/single/:thread_id', [thread_id => qr/[0-9]+/])
|
||||
->get('/:thread_page', [thread_page => qr/[0-9]+/], {thread_page => 1})
|
||||
$thread->under('/single/:thread_id', [thread_id => qr/\d+/])
|
||||
->get('/:thread_page', [thread_page => qr/\d+/], {thread_page => 1})
|
||||
->to('thread#by_id')
|
||||
->name('single_thread');
|
||||
|
||||
$thread->under('/bump')
|
||||
->get('/:thread_id', [thread_id => qr/[0-9]+/])
|
||||
->get('/:thread_id', [thread_id => qr/\d+/])
|
||||
->to('thread#bump')
|
||||
->name('bump_thread');
|
||||
|
||||
$thread->under('/flag')
|
||||
->get('/:thread_id', [thread_id => qr/[0-9]+/])
|
||||
->get('/:thread_id', [thread_id => qr/\d+/])
|
||||
->to('thread#flag')
|
||||
->name('flag_thread');
|
||||
|
||||
|
@ -110,35 +110,41 @@ sub startup($self) {
|
|||
my $remark = $r->under('/remark');
|
||||
|
||||
$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')
|
||||
->name('post_remark');
|
||||
|
||||
$remark->under('/single')
|
||||
->get('/:remark_id', [remark_id => qr/[0-9]+/])
|
||||
->get('/:remark_id', [remark_id => qr/\d+/])
|
||||
->to('remark#by_id')
|
||||
->name('single_remark');
|
||||
|
||||
$remark->under('/flag')
|
||||
->get('/:remark_id', [remark_id => qr/[0-9]+/])
|
||||
->get('/:remark_id', [remark_id => qr/\d+/])
|
||||
->to('remark#flag')
|
||||
->name('flag_remark');
|
||||
|
||||
# Login
|
||||
# Login/out
|
||||
$r->any([qw{GET POST}], '/login')
|
||||
->to('moderator#login')
|
||||
->name('mod_login');
|
||||
|
||||
$r->get('/logout')
|
||||
->to('moderator#logout')
|
||||
->name('mod_logout');
|
||||
|
||||
# Moderator
|
||||
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');
|
||||
|
||||
# Return false otherwise a body is rendered with the redirect...
|
||||
return undef;
|
||||
});
|
||||
|
||||
$moderator->get('/list')->to('moderator#list')->name('mod_list');
|
||||
$moderator->get('/list')
|
||||
->to('moderator#list')
|
||||
->name('mod_list');
|
||||
}
|
||||
|
||||
1;
|
||||
|
|
|
@ -7,10 +7,14 @@ sub list($self) { $self->render }
|
|||
sub login($self) {
|
||||
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';
|
||||
|
||||
if ($v && $v->has_data) {
|
||||
my ($email, $password);
|
||||
my ($email, $password, $mod_id, $mod_name);
|
||||
|
||||
$v->required('email' );
|
||||
$v->required('password');
|
||||
|
@ -22,12 +26,17 @@ sub login($self) {
|
|||
$email = $self->param('email' );
|
||||
$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)) {
|
||||
$self->session(moderator => 1);
|
||||
$self->session(mod_id => $mod_id);
|
||||
$self->flash(info => "Hello, $mod_name 😎");
|
||||
|
||||
return $self->redirect_to('mod_list');
|
||||
}
|
||||
else {
|
||||
$self->stash(status => 403);
|
||||
$self->flash(error => 'Invalid login! 🧐')
|
||||
}
|
||||
}
|
||||
|
@ -36,4 +45,12 @@ sub login($self) {
|
|||
$self->render;
|
||||
}
|
||||
|
||||
sub logout($self) {
|
||||
delete $self->session->{'mod_id'};
|
||||
|
||||
$self->flash(info => 'Logged out successfully 👋');
|
||||
|
||||
$self->redirect_to('threads_list');
|
||||
}
|
||||
|
||||
1;
|
||||
|
|
|
@ -19,4 +19,20 @@ sub check($self, $email, $password) {
|
|||
->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;
|
||||
|
|
|
@ -9,6 +9,7 @@
|
|||
<nav>
|
||||
<%= link_to List => 'threads_list' %>
|
||||
<%= link_to New => 'post_thread' %>
|
||||
<%= link_to Moderate => 'mod_list' %>
|
||||
</nav>
|
||||
<hr>
|
||||
<% if (flash 'error') { =%>
|
||||
|
|
|
@ -2,3 +2,4 @@
|
|||
% title 'Top Secret';
|
||||
<h2><%= title %></h2>
|
||||
<p>For mods only!!</p>
|
||||
<%= link_to Logout => 'mod_logout' %>
|
||||
|
|
Loading…
Reference in a new issue