diff --git a/lib/PostText.pm b/lib/PostText.pm index 51f7f5f..42fb24d 100644 --- a/lib/PostText.pm +++ b/lib/PostText.pm @@ -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; diff --git a/lib/PostText/Controller/Moderator.pm b/lib/PostText/Controller/Moderator.pm new file mode 100644 index 0000000..25d799d --- /dev/null +++ b/lib/PostText/Controller/Moderator.pm @@ -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; diff --git a/lib/PostText/Model/Moderator.pm b/lib/PostText/Model/Moderator.pm index 61df924..20b056c 100644 --- a/lib/PostText/Model/Moderator.pm +++ b/lib/PostText/Model/Moderator.pm @@ -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, diff --git a/templates/layouts/main.html.ep b/templates/layouts/main.html.ep index 3ed4baf..88b69cd 100644 --- a/templates/layouts/main.html.ep +++ b/templates/layouts/main.html.ep @@ -11,6 +11,9 @@ <%= link_to New => 'post_thread' %>
<%= flash 'error' %>
+<% } =%> <% if (flash 'info') { =%><%= flash 'info' %>
<% } =%> diff --git a/templates/moderator/index.html.ep b/templates/moderator/index.html.ep new file mode 100644 index 0000000..c7c03f6 --- /dev/null +++ b/templates/moderator/index.html.ep @@ -0,0 +1,4 @@ +% layout 'main'; +% title 'Top Secret'; +For mods only!!
diff --git a/templates/moderator/list.html.ep b/templates/moderator/list.html.ep new file mode 100644 index 0000000..c7c03f6 --- /dev/null +++ b/templates/moderator/list.html.ep @@ -0,0 +1,4 @@ +% layout 'main'; +% title 'Top Secret'; +For mods only!!
diff --git a/templates/moderator/login.html.ep b/templates/moderator/login.html.ep new file mode 100644 index 0000000..ac226bb --- /dev/null +++ b/templates/moderator/login.html.ep @@ -0,0 +1,14 @@ +% layout 'main'; +% title 'Moderator Login'; +