s/key/api_key/, update pod
[catagits/Catalyst-Authentication-Credential-FBConnect.git] / lib / Catalyst / Authentication / Credential / FBConnect.pm
index 33b0c07..c87785e 100644 (file)
@@ -1,43 +1,35 @@
 package Catalyst::Authentication::Credential::FBConnect;
-use strict;
-use warnings;
-
 use Moose;
-
-has _config => ( is => 'rw' );
-has debug => ( is => 'rw' );
-has key => ( is => 'rw' );
-has secret => ( is => 'rw' );
-has app_name => ( is => 'rw' );
-has fbconnect => ( is => 'rw' );
-
+use MooseX::Types::Moose qw/ Bool /;
+use MooseX::Types::Common::String qw/ NonEmptySimpleStr /;
 use WWW::Facebook::API;
 use Catalyst::Exception ();
+use namespace::autoclean;
 
-sub new {
-       my ($class, $config, $c, $realm) = @_;
+has debug => ( is => 'ro', isa => Bool, );
+has api_key => ( is => 'ro', isa => NonEmptySimpleStr, required => 1 );
+has secret => ( is => 'ro', isa => NonEmptySimpleStr, required => 1 );
+has app_name => ( is => 'ro', isa => NonEmptySimpleStr, required => 1 );
+has fbconnect => ( is => 'ro', lazy_build => 1, init_arg => undef, isa => 'WWW::Facebook::API' );
 
-       my $self = { _config => {
-               %{ $config },
-               %{ $realm->{config} }
-       } };
+sub BUILDARGS {
+       my ($class, $config, $c, $realm) = @_;
 
-       bless $self, $class;
+    return $config;
+}
 
-       $self->debug( $self->_config->{debug} );
+sub BUILD {
+    my ($self) = @_;
+    $self->fbconnect; # Ensure lazy value is built.
+}
 
-       $self->key( $self->_config->{key} );
-       $self->secret( $self->_config->{secret} );
-       $self->app_name( $self->_config->{app_name} );
+sub _build_fbconnect {
+    my $self = shift;
 
-       $self->fbconnect( WWW::Facebook::API->new(
+       WWW::Facebook::API->new(
                desktop => 0,
-               app_name => $self->app_name,
-               api_key => $self->key,
-               secret => $self->secret
-       ) );
-
-       return $self;
+               map { $_ => $self->$_() } qw/ app_name api_key secret /
+       );
 }
 
 sub authenticate {
@@ -66,8 +58,7 @@ sub authenticate {
                return;
        }
        else {
-
-               $c->res->redirect( $self->fbconnect->get_login_url( next => $c->uri_for( $c->action ) ) );
+               $c->res->redirect( $self->fbconnect->get_login_url( next => $c->uri_for( $c->action, $c->req->captures, @{ $c->req->args } ) ) );
        }
 
 }
@@ -98,19 +89,19 @@ In MyApp.pm
 
 In myapp.conf
 
- <Plugin::Authentication>
-      default_realm    facebook
-      <realms>
-           <facebook>
+    <Plugin::Authentication>
+        default_realm  facebook
+        <realms>
+            <facebook>
                 <credential>
-                     class     FBConnect
-               </credential>
-               key my_app_key
-                secret my_app_secret
-                app_name my_app_name
-           </facebook>
-      </realms>
-</Plugin::Authentication>
+                    class       FBConnect
+                    api_key     my_app_key
+                    secret      my_app_secret
+                    app_name    my_app_name
+                </credential>
+            </facebook>
+        </realms>
+    </Plugin::Authentication>
 
 
 In controller code,