Working login.

This commit is contained in:
Sergiotarxz 2023-11-20 20:20:04 +01:00
parent 1447b2fa6e
commit d6d827fe8d
4 changed files with 83 additions and 24 deletions

View File

@ -91,7 +91,8 @@ export default class Conquer {
Conquer.fail('Unable to find the submit button for the login.')
}
this.conquerLoginSubmit = conquerLoginSubmit
this.conquerLoginSubmit.addEventListener('click', () => {
this.conquerLoginSubmit.addEventListener('click', (event: Event) => {
event.preventDefault()
this.onLoginRequested()
});
}
@ -122,6 +123,10 @@ export default class Conquer {
return
}
this.unsetLoginAndRegisterErrors()
const isLogged = await this.isLogged();
if (isLogged) {
await this.removeLoginRegisterCombo()
}
}
async goToRegister(): Promise<void> {
@ -374,19 +379,21 @@ export default class Conquer {
constructor(conquerContainer: HTMLDivElement) {
this.conquerContainer = conquerContainer;
}
private async addNewLoginRegisterError(message: string): Promise<void> {
this.addNewRegisterError(message)
this.addNewLoginError(message)
}
private async isLogged(): Promise<boolean> {
return false
// return fetch("/conquer/user")
// .then(async (res): Promise<boolean> => {
// const data = await res.json();
// if (data === null) {
// return false;
// }
// return true;
// })
// .catch((error) => {
// console.error(error);
// return false;
// });
const urlUser = new URL('/conquer/user', window.location.protocol + '//' + window.location.hostname + ':' + window.location.port)
let responseJson;
let status
try {
const response = await fetch(urlUser)
status = response.status
} catch {
this.addNewLoginRegisterError('Error del servidor')
return false
}
return status === 200
}
}

View File

@ -19,26 +19,66 @@ sub startup ($self) {
);
push @{ $self->commands->namespaces }, 'BurguillosInfo::Command';
$self->hook(
before_render => sub($c, $args) {
before_render => sub ( $c, $args ) {
my $current_route = $c->url_for;
$c->stash(current_route => $current_route);
my $is_android = $c->req->headers->user_agent =~ /android/i;
$c->stash(is_android => $is_android);
$c->stash( current_route => $current_route );
my $is_android = $c->req->headers->user_agent =~ /android/i;
$c->stash( is_android => $is_android );
my $onion_base_url = $self->config->{onion_base_url};
my $base_url = $self->config->{base_url};
if (!defined $onion_base_url) {
my $base_url = $self->config->{base_url};
if ( !defined $onion_base_url ) {
return;
}
$current_route =~ s/^$base_url//;
$c->res->headers->header('Onion-Location' => $onion_base_url.$current_route);
$c->res->headers->header(
'Onion-Location' => $onion_base_url . $current_route );
}
);
my $config = $self->plugin('JSONConfig');
$self->config(
hypnotoad => { proxy => 1, listen => [$self->config('listen') // 'http://localhost:3000'] } );
hypnotoad => {
proxy => 1,
listen => [ $self->config('listen') // 'http://localhost:3000' ]
}
);
$self->config( css_version => int( rand(10000) ) );
$self->secrets( $self->config->{secrets} );
$self->helper(
current_user => sub ($c) {
use BurguillosInfo::Schema;
$self->session(expiration => 0);
my $user_uuid = $c->session->{conquer}{user};
if ( !defined $user_uuid ) {
return;
}
my $user_resultset =
BurguillosInfo::Schema->Schema->resultset('ConquerUser');
my @user_candidates =
$user_resultset->search( { uuid => $user_uuid } );
my $user = $user_candidates[0];
# Just to make clear what happens if there is no user we return.
if ( !defined $user ) {
return;
}
return $user;
}
);
$self->helper(
set_current_user => sub ( $c, $user ) {
$self->session(expiration => 0);
if ( !defined $user
|| !$user->can('uuid')
|| !$user->can('get_from_storage') )
{
die "$user is not a valid user for it's usage in a session.";
}
$user = $user->get_from_storage;
$c->session->{conquer}{user} = $user->uuid;
}
);
# Router
my $r = $self->routes;
@ -52,11 +92,13 @@ sub startup ($self) {
$r->get('/stats')->to('Metrics#stats');
$r->get('/conquer')->to('Conquer#index');
$r->put('/conquer/user')->to('UserConquer#create');
$r->get('/conquer/user')->to('UserConquer#get_self');
$r->post('/conquer/user/login')->to('UserConquer#login');
$r->get('/search.json')->to('Search#search');
$r->get('/farmacia-guardia.json')->to('FarmaciaGuardia#current');
$r->get('/<:category>.rss')->to('Page#category_rss');
$r->get('/:category_slug/atributo/<:attribute_slug>-preview.png')->to('Attribute#get_attribute_preview');
$r->get('/:category_slug/atributo/<:attribute_slug>-preview.png')
->to('Attribute#get_attribute_preview');
$r->get('/:category_slug/atributo/:attribute_slug')->to('Attribute#get');
$r->get('/<:category>-preview.png')->to('Page#get_category_preview');
$r->get('/:category')->to('Page#category');

View File

@ -21,6 +21,14 @@ my $username_maximum_chars = 15;
my $password_minimum_chars = 8;
my $password_maximum_chars = 4096;
sub get_self($self) {
my $user = $self->current_user;
if (!defined $user) {
return $self->_renderError(401, 'No estás loggeado.');
}
return $self->render(json => $user->serialize_to_owner, status => 200);
}
sub create ($self) {
my $input = $self->_expectJson;
if ( !defined $input ) {
@ -66,6 +74,8 @@ sub login ($self) {
$self->_renderError( 401, 'Contraseña incorrecta.' );
return;
}
my $user = $tentative_user;
$self->set_current_user($user);
$self->render(
json => {
success => $JSON::true

File diff suppressed because one or more lines are too long