Adding first login.

This commit is contained in:
sergiotarxz 2021-06-06 00:19:57 +02:00
parent 9913b7e421
commit 23cda8fb19
Signed by: sergiotarxz
GPG Key ID: E5903508B6510AC2
7 changed files with 51 additions and 19 deletions

View File

@ -10,9 +10,9 @@ WriteMakefile(
package MY { package MY {
sub top_targets { sub install {
my $self = shift; my $self = shift;
my $return = $self->SUPER::top_targets(@_); my $return = $self->SUPER::install(@_);
$return = [ split /\n/, $return ]; $return = [ split /\n/, $return ];
for my $i ( keys @$return ) { for my $i ( keys @$return ) {
$return->[$i] .= ' install_frontend_and_migrations' $return->[$i] .= ' install_frontend_and_migrations'

View File

@ -9,7 +9,7 @@ requires 'Params::ValidationCompiler';
requires 'Types::Standard'; requires 'Types::Standard';
requires 'Crypt::Bcrypt::Easy'; requires 'Crypt::Bcrypt::Easy';
requires 'DateTime'; requires 'DateTime';
requires 'DateTime::Format::ISO8601'; requires 'DateTime::Format::Pg';
requires 'Test::Most'; requires 'Test::Most';
requires 'Test::MockModule'; requires 'Test::MockModule';
requires 'Test::Warnings'; requires 'Test::Warnings';

View File

@ -13,8 +13,10 @@ use Mojo::File;
use Const::Fast; use Const::Fast;
use Params::ValidationCompiler 'validation_for'; use Params::ValidationCompiler 'validation_for';
use BeastBB::Constants ( 'HomeConfigDir', 'ConfigFile', use BeastBB::Constants (
'SecretDefaultSize' ); 'HomeConfigDir', 'ConfigFile',
'SecretDefaultSize'
);
use BeastBB::ConfigWriter; use BeastBB::ConfigWriter;
use BeastBB::Database; use BeastBB::Database;
use BeastBB::Response; use BeastBB::Response;
@ -48,12 +50,16 @@ sub PrepareHelpers {
} }
); );
$self->helper( $self->helper(
logged_user => sub { LoggedUser => sub {
my $self = shift; my $self = shift;
my $user_manager = BeastBB::DAO::UserManager->new( app => $self );
my $session = $self->session; my $session = $self->session;
return BeastBB::Response->new( is_error => 1,
error_message => 'User is not logged in.' );
my $username = $session->{username}; my $username = $session->{username};
return BeastBB::Response->new(
is_error => 1,
error_message => 'User is not logged in.'
) if !defined $username;
return $user_manager->Get( username => $username );
} }
); );
@ -89,8 +95,8 @@ sub PrepareCommonRoutes {
my $routes = $self->routes; my $routes = $self->routes;
$routes->get('/')->to('Main#Index'); $routes->get('/')->to('Main#Index');
$routes->get('/login')->to('Main#GetLogin'); $routes->get('/login')->to('Login#GetLogin');
$routes->post('/login')->to('Main#Login'); $routes->post('/login')->to('Login#Login');
$routes->post('/logout')->to('Main#Logout'); $routes->post('/logout')->to('Main#Logout');
} }

View File

@ -5,11 +5,21 @@ use 5.30.3;
use strict; use strict;
use warnings; use warnings;
use Mojo::Base 'Mojolicious::Controller'; use Mojo::Base 'BeastBB::Controller';
sub Index { sub Index {
my $self = shift; my $self = shift;
$self->stash( session => $self->session ); my $maybe_logged_in_user = $self->LoggedUser;
print Data::Dumper::Dumper $maybe_logged_in_user;
if ( $maybe_logged_in_user->IsError ) {
delete $self->session->{username}
if $maybe_logged_in_user =~ qr/No such user found\./;
}
$self->stash(
user => !$maybe_logged_in_user->IsError
? $maybe_logged_in_user->Content
: undef
);
$self->render; $self->render;
} }
1; 1;

View File

@ -13,7 +13,7 @@ use Types::Standard qw/Bool Str Int/;
use Crypt::Bcrypt::Easy; use Crypt::Bcrypt::Easy;
use Const::Fast; use Const::Fast;
use DateTime; use DateTime;
use DateTime::Format::ISO8601; use DateTime::Format::Pg;
use BeastBB::Types ( use BeastBB::Types (
'$MATRIX_ADDRESS_REGEX', 'IsClassTypeGenerator', '$MATRIX_ADDRESS_REGEX', 'IsClassTypeGenerator',
@ -158,10 +158,10 @@ const my $MINIMUM_PASSWORD_LENGHT => 8;
); );
} }
my $user_hash = $results->hash; my $user_hash = $results->hash;
$user_hash->{creation_date} = DateTime::Format::ISO8601->parse_datetime( $user_hash->{creation_date} = DateTime::Format::Pg->parse_datetime(
$user_hash->{creation_date} ); $user_hash->{creation_date} );
$user_hash->{last_connection} = $user_hash->{last_connection} =
DateTime::Format::ISO8601->parse_datetime( DateTime::Format::Pg->parse_datetime(
$user_hash->{last_connection} ); $user_hash->{last_connection} );
return BeastBB::Response->new( content => $user_hash ); return BeastBB::Response->new( content => $user_hash );

View File

@ -0,0 +1,13 @@
<h1>Login</h1>
<form method="post" action="/login">
<label for="username">Username</label>
<br/>
<input type="text" name="username"/>
<br/>
<label for="password">Password</label>
<br/>
<input type="text" name="password"/>
<br/>
<input type="submit" value="Login"/>
</form>

View File

@ -1 +1,4 @@
<h1>Hello world</h1> <h1>Welcome to BeastBB <%= defined $user ? $user->Username : 'anonymous user' %>.</h1>
% if (!defined $user) {
<p><a href="/login">Login</a> <a href="/signup">Signup</a>
% }