Skip to content

Commit

Permalink
Login form
Browse files Browse the repository at this point in the history
Now store_location actually works!
User auth etc
  • Loading branch information
castaway committed Apr 22, 2012
1 parent 407fbd1 commit 6dd03c1
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 28 deletions.
46 changes: 25 additions & 21 deletions geotrader.cgi
Expand Up @@ -6,32 +6,38 @@ use Plack::Middleware::Session;
use JSON;
use Data::Dumper;
use Web::Simple;
# use Moo;
use lib '/mnt/shared/projects/cardsapp/lib';
use GeoTrader;

sub dispatch_request {
my ($self) = @_;
has 'model' => (is => 'ro', lazy => 1, builder => '_build_model');

my $user;
my $gt = GeoTrader->new(
sub _build_model {
return GeoTrader->new(
base_uri => '/cgi-bin/geotrader.cgi',
app_cwd => 'mnt/shared/projects/cardsapp',
);
}

sub dispatch_request {
my ($self) = @_;

$self->check_authenticated($user, $gt),
my $user;

$self->check_authenticated($user),

sub (GET + /) {
my ($self) = @_;
## default/home page, check location, if near known node, display location page, else display users cards + "no places nearby"

return [ 200, [ 'Content-type', 'text/html' ], [ $self->default_page($gt) ]];
return [ 200, [ 'Content-type', 'text/html' ], [ $self->default_page() ]];
},

sub (POST + /login + %username=&password=) {
my ($self, $usern, $passw) = @_;

# print STDERR "user check $usern\n";
my $user = $gt->get_check_user($usern, $passw);
my $user = $self->model->get_check_user($usern, $passw);


if($user) {
Expand All @@ -49,9 +55,9 @@ sub dispatch_request {
my ($self, $usern, $passw, $display) = @_;

## FIXME: Check length of inputs!
$gt->create_user($usern, $passw, $display);
$self->model->create_user($usern, $passw, $display);

return [ 200, [ 'Content-type', 'text/html' ], [ $self->default_page($gt) ]];
return [ 200, [ 'Content-type', 'text/html' ], [ $self->default_page() ]];
},

sub (GET + /loc/*/*) {
Expand All @@ -60,7 +66,7 @@ sub dispatch_request {

my $test_location = [$lat, $long];

return [ 200, [ 'Content-type', 'text/html' ], [ default_page($gt, $test_location) ]];
return [ 200, [ 'Content-type', 'text/html' ], [ default_page($test_location) ]];
},

sub (GET + /places + ?bbox=) {
Expand All @@ -69,7 +75,7 @@ sub dispatch_request {
return [200, [ 'Content-type', 'text/plain' ], [''] ] if(!$bbox);

my @bounds = split(/,/, $bbox);
my $places = $gt->get_places(@bounds);
my $places = $self->model->get_places(@bounds);

return [200, [ 'Content-type', 'text/plain' ], [ $places || '' ] ];
},
Expand All @@ -85,9 +91,7 @@ sub dispatch_request {
return [200, [], ['']];
}

$user->latitude($lat);
$user->longitude($lon);
$user->update();
$user->update_location($lat, $lon);

return [200, [ 'Content-type', 'text/plain' ], [ 'Updated' ] ];
},
Expand All @@ -101,15 +105,15 @@ sub dispatch_request {
}

sub default_page {
my ($self, $gt, $loc) = @_;
my ($self, $loc) = @_;
my $user = $self->get_user();
my $user_location = $loc || $self->get_location();
my $place = $gt->find_place($user_location);
my $place = $self->model->find_place($user_location);
if($place) {
$gt->set_location($user, $place, $user_location);
$self->model->set_location($user, $place, $user_location);
}
my $page = $gt->get_user_profile() if($user && !$place);
$page = $gt->get_default_page if(!$user);
my $page = $self->model->get_user_profile() if($user && !$place);
$page = $self->model->get_default_page if(!$user);

return $place || $page;
}
Expand All @@ -134,13 +138,13 @@ sub set_authenticated {
}

sub check_authenticated {
my ($self, $gt) = @_;
my ($self) = @_;
my $user_ref = \$_[1];
return (
$self->ensure_session,
sub () {
if (my $uc = $_[PSGI_ENV]->{'psgix.session'}{'user_info'}) {
${$user_ref} = $gt->schema->resultset('User')->find($uc);
${$user_ref} = $self->model->schema->resultset('User')->find($uc);
}
return;
}
Expand Down
4 changes: 2 additions & 2 deletions gp.js
Expand Up @@ -122,7 +122,7 @@ GP.highlight_close_features = function(my_loc) {

if(is_changed) {
GP.places_layer.redraw();
// GP.store_location(my_loc);
GP.store_location(my_loc);
}

};
Expand Down Expand Up @@ -152,7 +152,7 @@ GP.store_location = function(point) {

jQuery.post(
'/cgi-bin/geotrader.cgi/_update_location',
{ lat: latlon.latitude, lon: latlon.longitude }
{ lat: latlon.lat, lon: latlon.lon }
);
}

Expand Down
21 changes: 16 additions & 5 deletions map-page.html
Expand Up @@ -12,11 +12,22 @@
<body>
<div id="log" style="display:block; height: 50px; overflow: scroll"></div>
<div id="stats" style="" ></div>
<ul id="minimenu">
<li><input type="checkbox" id="toggle_tracking" checked="checked" /> Toggle tracking</li>
<li><input type="checkbox" id="toggle_following" checked="checked" /> Toggle following</li>
<li><input type="button" id="profile" value="View Profile"></li>
</ul>
<!-- <div class="table"> -->
<ul id="minimenu">
<li><input type="checkbox" id="toggle_tracking" checked="checked" /> Toggle tracking</li>
<li><input type="checkbox" id="toggle_following" checked="checked" /> Toggle following</li>
<li><input type="button" id="profile" value="View Profile"></li>
</ul>
<ul id="login">
<li>
<form method="post" action="/cgi-bin/geotrader.cgi/login">
Username <input type="text" id="profile" name="username">
Password <input type="password" id="profile" name="password">
<input type="submit" value="Login">
</form>
</li>
</ul>
<!-- </div> -->
<div id="map" style="width:100%; height:600px"></div>
</body>
</html>

0 comments on commit 6dd03c1

Please sign in to comment.