Skip to content

Instantly share code, notes, and snippets.

@akuks
Last active March 26, 2020 04:51
Show Gist options
  • Save akuks/d4f2f2669e926e3b8c271bb35f20fe89 to your computer and use it in GitHub Desktop.
Save akuks/d4f2f2669e926e3b8c271bb35f20fe89 to your computer and use it in GitHub Desktop.
## App file
package API;
use Mojo::Base 'Mojolicious';
# Custom Packages
use API::Model::DB ;
use API::DBOperations;
use API::Common;
use API::Validator;
use Error;
use Carp;
sub startup {
my $self = shift;
# Load configuration from hash returned by config file
my $config = $self->plugin('Config');
$self->secrets(['Mojo is awesome']);
# Set database connection
$self->_set_db_operation_handler();
$self->_set_image_size($config);
$self->_get_api_key($config); #Get API Key
$self->{_validator} = API::Validator->new( _error => Error->new() );
# Configure the application
$self->secrets($config->{secrets});
# Router
my $r = $self->routes;
$self->hook(after_dispatch => sub {
my $c = shift;
$c->res->headers->header('Access-Control-Allow-Origin' => '*');
$c->res->headers->access_control_allow_origin('*');
$c->res->headers->header('Access-Control-Allow-Methods' => 'GET, OPTIONS, POST, DELETE, PUT');
$c->res->headers->header('Access-Control-Allow-Headers' => 'Content-Type' => 'application/x-www-form-urlencoded');
});
# Auth routes
my $auth = $r->post('/auth');
$auth->post('/register')->to(
controller => 'Auth::RegisterController', action => 'user_registration'
);
$auth->post('/login')->to(
controller => 'Auth::LoginController', action => 'login', self => $self
);
$auth->post('/logout')->to(
controller => 'Auth::LoginController', action => 'logout'
);
$auth->post('/profile')->to(
controller => 'Auth::ProfileController', action => 'get_profile'
);
$auth->post('/profile/update')->to(
controller => 'Auth::ProfileController', action => 'update'
);
}
package LoginController;
sub login {
my $c = shift;
my $self = $c->stash('self');
my $db_object = $self->app->{ _dbOH };
my $username = $c->param('username');
my $password = $c->param('password');
$self->plugin('authentication' => {
autoload_user => 1,
wickedapp => 'YouAreLogIn',
load_user => sub {
my ($c, $user_key) = @_;
my $user = $db_object->get_user_details($user_key);
my $token = 'JustToTest';
return $user;
},
validate_user => sub {
my ($c, $username, $password) = @_;
my $user_key = $db_object->validate_user_login($username, $password);
if ( $user_key ) {
$c->session(user => $user_key);
return $user_key;
}
else {
return undef;
}
},
});
my $auth_key = $c->authenticate($username, $password );
if ( $auth_key ) {
return $c->render(
json =>
{
message => 'Login Success.',
is_login_sucess => 1,
login_key => 'MyAweSomeToken' ,
user_details => $c->stash('__authentication__')->{user},
status => 200
},
);
}
else {
$c->render(
json =>
{
message => 'Invalid username or password.',
is_login_success => 0
} ,
);
}
}
@akuks
Copy link
Author

akuks commented Mar 26, 2020

( {
'names' => {
'access-control-allow-headers' => 'Access-Control-Allow-Headers',
'access-control-allow-methods' => 'Access-Control-Allow-Methods'
},
'headers' => {
'access-control-allow-methods' => [
'GET, OPTIONS, POST, DELETE, PUT'
],
'access-control-allow-origin' => [
'*'
],
'server' => [
'Mojolicious (Perl)'
],
'access-control-allow-headers' => [
'Content-Type',
'application/x-www-form-urlencoded'
],
'content-type' => [
'text/html;charset=UTF-8'
]
}
}, 'Mojo::Headers' );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment