From d7ec11d4ff1467f61b7fc5936cebcf158ef1765a Mon Sep 17 00:00:00 2001 From: Struan Donald Date: Fri, 1 Jun 2012 16:38:09 +0100 Subject: inital pass at sign in/sign out functionality --- perllib/FixMyStreet/App/Controller/Auth.pm | 49 ++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) (limited to 'perllib/FixMyStreet/App/Controller/Auth.pm') diff --git a/perllib/FixMyStreet/App/Controller/Auth.pm b/perllib/FixMyStreet/App/Controller/Auth.pm index c67de692a..3dc25dedf 100644 --- a/perllib/FixMyStreet/App/Controller/Auth.pm +++ b/perllib/FixMyStreet/App/Controller/Auth.pm @@ -7,6 +7,7 @@ BEGIN { extends 'Catalyst::Controller'; } use Email::Valid; use Net::Domain::TLD; use mySociety::AuthToken; +use JSON; =head1 NAME @@ -250,6 +251,54 @@ sub sign_out : Local { $c->logout(); } +sub ajax_sign_in : Path('ajax/sign_in') { + my ( $self, $c ) = @_; + + my $return = {}; + if ( $c->forward( 'sign_in' ) ) { + $return->{name} = $c->user->name; + } else { + $return->{error} = 1; + } + + my $body = JSON->new->utf8(1)->encode( $return ); + $c->res->content_type('application/json; charset=utf-8'); + $c->res->body($body); + + return 1; +} + +sub ajax_sign_out : Path('ajax/sign_out') { + my ( $self, $c ) = @_; + + $c->logout(); + + my $body = JSON->new->utf8(1)->encode( { signed_out => 1 } ); + $c->res->content_type('application/json; charset=utf-8'); + $c->res->body($body); + + return 1; +} + +sub ajax_check_auth : Path('ajax/check_auth') { + my ( $self, $c ) = @_; + + my $code = 401; + my $data = { not_authorized => 1 }; + + if ( $c->user ) { + $data = { name => $c->user->name }; + $code = 200; + } + + my $body = JSON->new->utf8(1)->encode( $data ); + $c->res->content_type('application/json; charset=utf-8'); + $c->res->code($code); + $c->res->body($body); + + return 1; +} + =head2 check_auth Utility page - returns a simple message 'OK' and a 200 response if the user is -- cgit v1.2.3