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 {
sub top_targets {
sub install {
my $self = shift;
my $return = $self->SUPER::top_targets(@_);
my $return = $self->SUPER::install(@_);
$return = [ split /\n/, $return ];
for my $i ( keys @$return ) {
$return->[$i] .= ' install_frontend_and_migrations'

View File

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

View File

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

View File

@ -5,11 +5,21 @@ use 5.30.3;
use strict;
use warnings;
use Mojo::Base 'Mojolicious::Controller';
use Mojo::Base 'BeastBB::Controller';
sub Index {
my $self = shift;
$self->stash( session => $self->session );
my $self = shift;
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;
}
1;

View File

@ -13,7 +13,7 @@ use Types::Standard qw/Bool Str Int/;
use Crypt::Bcrypt::Easy;
use Const::Fast;
use DateTime;
use DateTime::Format::ISO8601;
use DateTime::Format::Pg;
use BeastBB::Types (
'$MATRIX_ADDRESS_REGEX', 'IsClassTypeGenerator',
@ -158,10 +158,10 @@ const my $MINIMUM_PASSWORD_LENGHT => 8;
);
}
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->{last_connection} =
DateTime::Format::ISO8601->parse_datetime(
DateTime::Format::Pg->parse_datetime(
$user_hash->{last_connection} );
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>
% }