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


Catalyst-Authentication-Credential-Facebook documentation Contained in the Catalyst-Authentication-Credential-Facebook distribution.

Index


Code Index:

NAME

Top

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

SYNOPSIS

Top

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",
                 },
             },
         },
     },
 );

And then in your Controller:

 sub login : Local {
    my ($self, $c) = @_;

	if (my $user = $c->authenticate(undef,'facebook')) {
		# user is logged in - redirect or do something
	}
	else {
		# user has no account in your system
		# detect Facebook credentials and create an account
		# or do comething else
	} 
 }

DESCRIPTION

Top

This module handles Facebook Platform authentication in a Catalyst application.

METHODS

Top

As per guidelines of Catalyst::Plugin::Authentication, there are two mandatory methods, new and authenticate.

new()

Will not be called by you directly, but will use the configuration you provide (see above). WWW::Facebook::API is required, but we also suggest you install Catalyst::Plugin::Facebook for ultimate Facebook integration after the user is authenticated.

authenticate( )

Handles the authentication. Nothing more, nothing less. It returns a Catalyst::Authentication::User::Hash (this is a user object from your database - the user table should have a column called "facebook_id", which the authenticated Facebook user is checked against)

AUTHOR

Top

Jesse Stay <jesse@staynalive.com> http://staynalive.com

COPYRIGHT

Top

SEE ALSO

Top

Catalyst::Plugin::Authentication, WWW::Facebook::API

BUGS

Top

Bugs? Impossible!. Please report bugs to http://rt.cpan.org/Ticket/Create.html?Queue=Catalyst-Authentication-Credential-Facebook

THANKS

Top

Special thanks to David Romano and Clayton Scott for writing and maintaining WWW::Facebook::API


Catalyst-Authentication-Credential-Facebook documentation Contained in the Catalyst-Authentication-Credential-Facebook distribution.

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;
}
    
1;