Skip to content

Commit

Permalink
Add /logout
Browse files Browse the repository at this point in the history
  • Loading branch information
castaway committed Apr 27, 2012
1 parent 1dad8de commit a34157a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
44 changes: 38 additions & 6 deletions geotrader.cgi
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,25 @@ use GoingPlaces::View;
has 'model' => (is => 'ro', lazy => 1, builder => '_build_model');
has 'view' => (is => 'ro', lazy => 1, builder => '_build_view');

has 'host' => (is => 'ro', default => 'http://desert-island.me.uk');
has 'base_uri' => (is => 'ro', default => '/cgi-bin/geotrader.cgi');
has 'app_cwd' => ( is => 'ro', default => '/mnt/shared/projects/cardsapp');
has 'static_uri' => ( is => 'ro', default => '/~castaway/cardsapp');

sub _build_model {
my ($self) = @_;
return GoingPlaces::Model->new(
base_uri => '/cgi-bin/geotrader.cgi',
app_cwd => '/mnt/shared/projects/cardsapp',
base_uri => $self->base_uri,
app_cwd => $self->app_cwd,
);
}

sub _build_view {
my ($self) = @_;
return GoingPlaces::View->new(
base_uri => '/cgi-bin/geotrader.cgi',
static_uri => '/~castaway/cardsapp',
app_cwd => '/mnt/shared/projects/cardsapp',
base_uri => $self->base_uri,
static_uri => $self->static_uri,
app_cwd => $self->app_cwd,
);
}

Expand Down Expand Up @@ -57,12 +64,27 @@ sub dispatch_request {
if($user) {
# Turtles all the way down!
return ($self->set_authenticated($user),
[ 200, [ 'Content-type', 'text/html' ], [ 'Login succeeded' ]]);
[ 303, [ 'Content-type', 'text/html',
'Location', $self->host . $self->base_uri . '/map' ],
[ 'Login succeeded, back to <a href="' . $self->host . $self->base_uri . '/map' . '"></a>' ]]);
} else {
return [ 200, [ 'Content-type', 'text/html' ], [ 'Login failed' ]];
}
},

sub (GET + /logout) {
my ($self) = @_;

if($user) {
$user = undef;
}

return ($self->logout,
[ 303, [ 'Content-type', 'text/html',
'Location', $self->host . $self->base_uri . '/map' ],
[ 'Login succeeded, back to <a href="' . $self->host . $self->base_uri . '/map' . '"></a>' ]]);
},

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

Expand Down Expand Up @@ -164,6 +186,16 @@ sub set_authenticated {
);
}

sub logout {
my ($self) = @_;
return (
$self->ensure_session,
sub {
delete $_[PSGI_ENV]->{'psgix.session'}{user_info};
}
);
}

sub check_authenticated {
my ($self) = @_;
my $user_ref = \$_[1];
Expand Down
4 changes: 2 additions & 2 deletions templates/map_page.tt
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
<ul id="login">
<li>
[% IF !user %]
<form method="post" action="/cgi-bin/geotrader.cgi/login">
<form method="post" action="[% base_uri %]/login">
Username <input type="text" id="profile" name="username">
Password <input type="password" id="profile" name="password">
<input type="submit" value="Login">
</form>
[% ELSE %]
Hello again, [% user.display_name %] !
Hello again, [% user.display_name %] ! | <a href="[% base_uri %]/logout">Logout</a>
[% END %]
</li>
</ul>
Expand Down

0 comments on commit a34157a

Please sign in to comment.