Catalyst-Authentication-Credential-Facebook

 view release on metacpan or  search on metacpan

lib/Catalyst/Authentication/Credential/Facebook.pm  view on Meta::CPAN

package Catalyst::Authentication::Credential::Facebook;

use strict;
use warnings;
use base qw( Class::Accessor::Fast );
use Data::Dumper;

BEGIN {
    __PACKAGE__->mk_accessors(qw/_facebook/);
}

our $VERSION = "0.01";

use Catalyst::Exception ();
use WWW::Facebook::API;

sub new {
    my ($class, $config, $c, $realm) = @_;
    my $self = {};
    bless $self, $class;

    # Hack to make lookup of the configuration parameters less painful
    my $params = { %{ $config }, %{ $realm->{config} } };

    # Create a WWW::Facebook::API instance
    $self->_facebook(
		WWW::Facebook::API->new(
			'desktop'	=> 0,
			'format'	=> 'JSON',
			'parse'		=> 1,
			%{ $c->config->{'facebook'} || { } },
		)
	);

    return $self;
}

sub authenticate {
    my ( $self, $c, $realm, $authinfo ) = @_;

	$self->_facebook->query( $c->request );

	# get facebook_id if there is one
	my $fb_user = $self->_facebook->canvas->get_fb_params();
	$c->log->debug('facebook_id from facebook: '.$fb_user->{'user'});

	if (!$authinfo || $fb_user->{'user'} && !$authinfo->{'facebook_id'}) { $authinfo->{'facebook_id'} = $fb_user->{'user'}; }

	if ( $authinfo->{'facebook_id'} ) {
		my $user_obj = $realm->find_user($authinfo, $c);

		if (ref $user_obj) {
			return $user_obj;
		}
	}

    return undef;
}
    
=head1 NAME

Catalyst::Authentication::Credential::Facebook - Facebook authentication for Catalyst

=head1 SYNOPSIS

In MyApp.pm

 use Catalyst qw/
    Authentication
    Session
    Session::Store::FastMmap
    Session::State::Cookie
	Facebook
 /;
 
 MyApp->config(
     "Plugin::Authentication" => {
         default_realm => "facebook",
         realms => {
             'facebook' => {
                 credential => {
                     class => "Facebook",
                 },
             },
         },
     },
 );



( run in 1.717 second using v1.01-cache-2.11-cpan-d06a3f9ecfd )