Added check to see if key has been used

This commit is contained in:
piratefinn 2016-09-01 13:30:08 +01:00
parent f9e850dff9
commit 45bfa2878c
2 changed files with 21 additions and 15 deletions

View file

@ -4,6 +4,7 @@
use Mojolicious::Lite; use Mojolicious::Lite;
use Data::UUID; use Data::UUID;
use Devel::Dwarn; use Devel::Dwarn;
use Mojo::JSON;
# connect to database # connect to database
use DBI; use DBI;
@ -32,9 +33,16 @@ post '/' => sub {
my $self = shift; my $self = shift;
# get the key from user # get the key from user
my $key = $self->req->json; my $key = $self->req->json;
# Check if that token key has been used before
my $keyused = $dbh->selectall_arrayref("SELECT keyused FROM accounts WHERE idkey = ?", undef, $key->{token});
# if the key has been used before, tell the user to sod off
unless ($keyused != t) {
print "The key $key has already been used!";
return $self->render(json => {'success' => Mojo::JSON->false});
}
# get from db the username matching the key and then send it back at them # get from db the username matching the key and then send it back at them
my $username = $dbh->selectall_arrayref("SELECT username FROM accounts WHERE idkey = ?", undef, $key->{token}); my $username = $dbh->selectall_arrayref("SELECT username FROM accounts WHERE idkey = ?", undef, $key->{token});
$self->render(json => {'username' => $username->[0]} ); $self->render(json => {'username' => $username->[0], success => Mojo::JSON->true} );
# When user has submitted json of data, define data # When user has submitted json of data, define data
my $name = $self->req->json; my $name = $self->req->json;
my $email = $self->req->json; my $email = $self->req->json;

18
senddatatodb.pl.example Normal file → Executable file
View file

@ -24,22 +24,18 @@ while (1) {
$insert = eval { $dbh->prepare('INSERT INTO foodloop (username, company, currency, filename) VALUES (?,?,?,?)') }; $insert = eval { $dbh->prepare('INSERT INTO foodloop (username, company, currency, filename) VALUES (?,?,?,?)') };
# break out of loop if statement prepared # break out of loop if statement prepared
last if $insert; last if $insert;
print "Creating new Table"; print "Make the table!";
# if statement didn't prepare, assume its because the table doesn't exist
# warn "Creating table 'foodloop'\n";
# $dbh->do('CREATE TABLE foodloop (
# username varchar(255),
# company varchar(255),
# currency integer,
#filename varchar(255)
#);'
);
} }
# setup route which receives data and returns to / # setup route which receives data and returns to /
post '/' => sub { post '/' => sub {
my $self = shift; my $self = shift;
# Fetch parameters to write to DB # Fetch parameters to write to DB
my $key = $self->param('key');
# This will include an if function to see if key matches
# unless ($key eq $config->{key}) {
# print "key does not match!";
# }
my $username = $self->param('username'); my $username = $self->param('username');
my $company = $self->param('company'); my $company = $self->param('company');
my $currency = $self->param('currency'); my $currency = $self->param('currency');
@ -54,7 +50,9 @@ post '/' => sub {
my $ext = '.jpg'; my $ext = '.jpg';
my $uuid = Data::UUID->new->create_str; my $uuid = Data::UUID->new->create_str;
my $filename = $uuid . $ext; my $filename = $uuid . $ext;
# send photo to image folder on server
$file->move_to('images/' . $filename); $file->move_to('images/' . $filename);
# send data to foodloop db
$insert->execute($username, $company, $currency, $filename); $insert->execute($username, $company, $currency, $filename);
$self->render(text => 'It did not kaboom!'); $self->render(text => 'It did not kaboom!');
}; };