From 4a59dd69dc1e410af8685601c53dc5ae842f9f2c Mon Sep 17 00:00:00 2001 From: swag Date: Sat, 19 Nov 2022 23:55:03 -0500 Subject: [PATCH] Implement login page --- lib/PostText.pm | 16 ++++++++++++ lib/PostText/Controller/Moderator.pm | 39 ++++++++++++++++++++++++++++ lib/PostText/Model/Moderator.pm | 2 +- templates/layouts/main.html.ep | 3 +++ templates/moderator/index.html.ep | 4 +++ templates/moderator/list.html.ep | 4 +++ templates/moderator/login.html.ep | 14 ++++++++++ 7 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 lib/PostText/Controller/Moderator.pm create mode 100644 templates/moderator/index.html.ep create mode 100644 templates/moderator/list.html.ep create mode 100644 templates/moderator/login.html.ep 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' %>
+<% if (flash 'error') { =%> +

<%= 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'; +

<%= title %>

+

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'; +

<%= title %>

+

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'; +

<%= title %>

+
+ +
+ <%= label_for password => 'Password' %> + <%= password_field 'password' %> +
+ <%= submit_button 'Login' %> +