package Daje::Controller::Users::Login;
use Mojo::Base 'Mojolicious::Controller', -signatures;
use v5.42;


# NAME
# ====
#
# Daje::Controller::Login::Login - It's new controller to manage login
#
# SYNOPSIS
# ========
#
#     use Daje::Controller::Login::Login;
#
# DESCRIPTION
# ===========
#
# Daje::Controller::Login::Login is ...
#
# LICENSE
# =======
#
# Copyright (C) janeskil1525.
#
# This library is free software; you can redistribute it and/or modify
# it under the same terms as Perl itself.
#
# AUTHOR
# ======
#
# janeskil1525 E<lt>janeskil1525@gmail.comE<gt>
#
use Data::Dumper;
use Mojo::JSON qw{from_json decode_json};

our $VERSION = "0.01";

sub login_user($self) {

    $self->app->log->debug('Daje::Controller::Users::Login::login_user '  . Dumper($self->req->body));
    $self->render_later;

    my $data = from_json($self->req->body);
    $self->login->login_user($data->{mail}, $data->{password})->then(sub ($result) {
        if(defined $result) {
            $self->render(json => {'result' => "success", data => $result});
        } else {
            $self->render(json => {'result' => "failed", data => {}});
        }
    })->catch(sub ($err) {
        say "Error " . $err;
        $self->render(json => {'result' => "failed", data => $err});
    })->wait;
}

sub check_verify($self) {

    $self->render_later;
    $self->app->log->debug('Daje::Controller::Users::Login::check_verify ' . Dumper($self->req->body));
    my $data = from_json($self->req->body);
    $self->login->check_verify($data->{mail}, $data->{code})->then(sub ($result) {
        if(defined $result and $result == 1) {
            $self->render(json => {'result' => 'success', verified => 'true'});
        } else {
            $self->render(json => {'result' => 'success', , verified => 'false'});
        }
    })->catch(sub ($err) {
        say "Error " . $err;
        $self->render(json => {'result' => $err});
    })->wait;
}

sub post_login($self) {

    $self->render_later;
    $self->app->log->debug('Daje::Controller::Users::Login::post_login ' . Dumper($self->req->body));
    my $data = from_json($self->req->body);
    $self->login->post_login($data)->then(sub ($result) {
        $self->render(json => {'result' => 'success', data => $result});
    })->catch(sub ($err) {
        say "Error " . $err;
        $self->render(json => {'result' => $err});
    })->wait;
}

sub signup($self) {

    $self->app->log->debug('Daje::Controller::Users::Login::signup ' . Dumper($self->req->body));
    my $data->{context} = decode_json( $self->req->body );
    try {
        $self->workflow_engine->workflow_pkey(0);
        $self->workflow_engine->workflow_name('users_users');
        $self->workflow_engine->context($data);
        $self->workflow_engine->process('register_new_users_user');
        if($self->workflow_engine->error->has_error() == 0) {
            $self->render(json => {result => 1, data => 'OK'});
        } else {
            $self->app->log->error('Daje::Controller::Users::Login::signup ' . $self->workflow_engine->error->error());
            $self->render(json =>
                {result => 0, data => $self->workflow_engine->error->error()}
            );
        }
    } catch ($e) {
        $self->app->log->error('Daje::Controller::Users::Login::signup ' . $e);
        $self->render(json => {result => 0, data => $e});
    };
    $self->app->log->debug('Daje::Controller::Users::Login::signup ends');
}

sub verify($self) {

    $self->app->log->debug('Daje::Controller::Users::Login::verify '  . Dumper($self->req->body));
    my $data->{context} = decode_json $self->req->body;
    try {
        say Dumper($data);
        $self->workflow_engine->workflow_pkey($data->{context}->{payload}->{users_workflow_fkey});
        $self->workflow_engine->workflow_name('users_users');
        $self->workflow_engine->context($data);
        $self->workflow_engine->process('verify_users_user');
        if($self->workflow_engine->error->has_error() == 0) {
            $self->render(json => {result => 1, data => 'OK'});
        } else {
            $self->app->log->error('Daje::Controller::Users::Login::verify ' . $self->workflow_engine->error->error());
            $self->render(json =>
                {result => 0, data => $self->workflow_engine->error->error()}
            );
        }
    } catch ($e) {
        $self->app->log->error('Daje::Controller::Users::Login::verify ' . $e);
        $self->render(json => {result => 0, data => $e});
    };
    $self->app->log->debug('Daje::Controller::Users::Login::verify ends');
}
1;

1;
__END__





#################### pod generated by Pod::Autopod - keep this line to make pod updates possible ####################

=head1 NAME


Daje::Controller::Login - It's new controller to manage login



=head1 SYNOPSIS


    use Daje::Controller::Login;



=head1 DESCRIPTION


Daje::Controller::Login is ...



=head1 REQUIRES

L<Mojo::JSON> 

L<Data::Dumper> 

L<v5.40> 

L<Mojo::Base> 


=head1 METHODS

=head2 login_user($self)

 login_user($self)();


=head1 AUTHOR


janeskil1525 E<lt>janeskil1525@gmail.comE<gt>



=head1 LICENSE


Copyright (C) janeskil1525.

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.



=cut

