From a549732ce74f8f0ffb9535374572ea2fbaf12c66 Mon Sep 17 00:00:00 2001 From: sergiotarxz Date: Sun, 6 Jun 2021 02:22:19 +0200 Subject: [PATCH] Adding logout. --- lib/BeastBB.pm | 2 +- lib/BeastBB/Controller/Login.pm | 17 +++++++++++++---- templates/main/Index.html.ep | 13 +++++++++++++ 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/lib/BeastBB.pm b/lib/BeastBB.pm index ba85d9c..a962538 100644 --- a/lib/BeastBB.pm +++ b/lib/BeastBB.pm @@ -97,7 +97,7 @@ sub PrepareCommonRoutes { $routes->get('/')->to('Main#Index'); $routes->get('/login')->to('Login#GetLogin'); $routes->post('/login')->to('Login#Login'); - $routes->post('/logout')->to('Main#Logout'); + $routes->post('/logout')->to('Login#Logout'); } sub PrepareInstallationRoutes { diff --git a/lib/BeastBB/Controller/Login.pm b/lib/BeastBB/Controller/Login.pm index f44d5c0..9cc093f 100644 --- a/lib/BeastBB/Controller/Login.pm +++ b/lib/BeastBB/Controller/Login.pm @@ -8,13 +8,14 @@ use warnings; use Mojo::Base 'BeastBB::Controller'; sub GetLogin { - my $self =shift; + my $self = shift; $self->render; } sub Login { - my $self = shift; - my $error_url = Mojo::URL->new('/')->query( error => 'You are already logged in.'); + my $self = shift; + my $error_url = + Mojo::URL->new('/')->query( error => 'You are already logged in.' ); return $self->redirect_to($error_url) if !$self->LoggedUser->IsError; my $user_manager = BeastBB::DAO::UserManager->new( app => $self ); my $username = $self->param('username'); @@ -23,11 +24,19 @@ sub Login { return $self->reply->exception('Request not understood.')->rendered(400) if !defined $username || !defined $password; my $maybe_user = $user_manager->Get( username => $username ); - $error_url = Mojo::URL->new('/')->query( error => $wrong_auth_error ); + $error_url = Mojo::URL->new('/')->query( error => $wrong_auth_error ); return $self->redirect_to($error_url) if $maybe_user->IsError || !$maybe_user->Content->CheckPasswordLogin( password => $password ); $self->session->{username} = $username; return $self->redirect_to('/'); } + +sub Logout { + my $self = shift; + $self->reply->exception('You are not logged in.')->rendered(401) + if $self->LoggedUser->IsError; + delete $self->session->{username}; + $self->redirect_to('/'); +} 1; diff --git a/templates/main/Index.html.ep b/templates/main/Index.html.ep index 241a920..62f37c7 100644 --- a/templates/main/Index.html.ep +++ b/templates/main/Index.html.ep @@ -1,7 +1,20 @@ + + + BeastBB + + +

Welcome to BeastBB <%= defined $user ? $user->Username : 'anonymous user' %>.

% if (!defined $user) {

Login Signup % } +% if (defined $user) { +

+ +
+% } % if (defined $error) {

<%= $error %>

% } + +