[Jifty-commit] r3605 - in jifty/trunk: lib/Jifty/Plugin/Authentication lib/Jifty/Plugin/Authentication/Facebook lib/Jifty/Plugin/Authentication/Facebook/Action

jifty-commit at lists.jifty.org jifty-commit at lists.jifty.org
Tue Jul 3 03:22:47 EDT 2007


Author: trs
Date: Tue Jul  3 03:22:46 2007
New Revision: 3605

Added:
   jifty/trunk/lib/Jifty/Plugin/Authentication/Facebook/Action/LinkFacebookUser.pm
Modified:
   jifty/trunk/   (props changed)
   jifty/trunk/lib/Jifty/Plugin/Authentication/Facebook.pm
   jifty/trunk/lib/Jifty/Plugin/Authentication/Facebook/Dispatcher.pm
   jifty/trunk/lib/Jifty/Plugin/Authentication/Facebook/View.pm

Log:
 r24978 at zot:  tom | 2007-07-03 03:22:01 -0400
 Provide a way to link existing users with a Facebook account


Modified: jifty/trunk/lib/Jifty/Plugin/Authentication/Facebook.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Plugin/Authentication/Facebook.pm	(original)
+++ jifty/trunk/lib/Jifty/Plugin/Authentication/Facebook.pm	Tue Jul  3 03:22:46 2007
@@ -93,4 +93,20 @@
     return $self->api->get_login_url( next => $next );
 }
 
+=head2 get_link_url
+
+Gets the login URL used for linking, preserving continuations
+
+=cut
+
+sub get_link_url {
+    my $self = shift;
+    my $next = '/facebook/callback_link';
+ 
+    if ( Jifty->web->request->continuation ) {
+        $next .= '?J:C=' . Jifty->web->request->continuation->id;
+    }
+    return $self->api->get_login_url( next => $next );
+}
+
 1;

Added: jifty/trunk/lib/Jifty/Plugin/Authentication/Facebook/Action/LinkFacebookUser.pm
==============================================================================
--- (empty file)
+++ jifty/trunk/lib/Jifty/Plugin/Authentication/Facebook/Action/LinkFacebookUser.pm	Tue Jul  3 03:22:46 2007
@@ -0,0 +1,75 @@
+use warnings;
+use strict;
+
+=head1 NAME
+
+Jifty::Plugin::Authentication::Facebook::Action::LinkFacebookUser;
+
+=cut
+
+package Jifty::Plugin::Authentication::Facebook::Action::LinkFacebookUser;
+use base qw/Jifty::Action/;
+
+=head1 ARGUMENTS
+
+=head2 auth_token
+
+=cut
+
+use Jifty::Param::Schema;
+use Jifty::Action schema {
+    param auth_token =>
+        type is 'text',
+        is mandatory;
+};
+
+=head1 METHODS
+
+=head2 take_action
+
+Get the session key using the Facebook API.  Link to current user.
+
+=cut
+
+sub take_action {
+    my $self     = shift;
+    my ($plugin) = Jifty->find_plugin('Jifty::Plugin::Authentication::Facebook');
+    my $api      = $plugin->api;
+
+    if ( not Jifty->web->current_user->id ) {
+        $self->result->error(_("You must be logged in to link your user to your Facebook account."));
+        return;
+    }
+
+    # Get the session
+    $api->auth->get_session( $self->argument_value('auth_token') );
+
+    my $user = Jifty->web->current_user->user_object;
+
+    my $name = $api->users->get_info(
+        uids    => $api->session_uid,
+        fields  => 'name'
+    )->[0]{'name'};
+
+    # Set data
+    $user->__set( column => 'facebook_name', value => $name );
+    $user->__set( column => 'facebook_uid',  value => $api->session_uid );
+    $user->__set( column => 'facebook_session', value => $api->session_key );
+    $user->__set( column => 'facebook_session_expires', value => $api->session_expires );
+
+    # Success!
+    $self->report_success;
+
+    return 1;
+}
+
+=head2 report_success
+
+=cut
+
+sub report_success {
+    my $self = shift;
+    $self->result->message(_("Your account has been successfully linked to your Facebook user %1!", Jifty->web->current_user->user_object->facebook_name ));
+}
+
+1;

Modified: jifty/trunk/lib/Jifty/Plugin/Authentication/Facebook/Dispatcher.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Plugin/Authentication/Facebook/Dispatcher.pm	(original)
+++ jifty/trunk/lib/Jifty/Plugin/Authentication/Facebook/Dispatcher.pm	Tue Jul  3 03:22:46 2007
@@ -20,10 +20,14 @@
 
 =cut
 
-before '/facebook/callback' => run {
+before qr'^/facebook/callback(_link)?' => run {
+    my $link    = $1 ? 1 : 0;
+    my $action  = $link ? 'LinkFacebookUser' : 'LoginFacebookUser';
+    my $moniker = $link ? 'facebooklink'     : 'facebooklogin';
+
     Jifty->web->request->add_action(
-        moniker   => 'facebooklogin',
-        class     => 'LoginFacebookUser',
+        moniker   => $moniker,
+        class     => $action,
         arguments => {
             auth_token => get('auth_token'),
         }

Modified: jifty/trunk/lib/Jifty/Plugin/Authentication/Facebook/View.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Plugin/Authentication/Facebook/View.pm	(original)
+++ jifty/trunk/lib/Jifty/Plugin/Authentication/Facebook/View.pm	Tue Jul  3 03:22:46 2007
@@ -14,8 +14,7 @@
 
 =head2 /facebook/login
 
-Calling C<show> on this fragment will include the standard Facebook button
-used for web login.
+This fragment shows the standard Facebook button used for web login.
 
 =cut
 
@@ -29,4 +28,22 @@
     };
 };
 
+=head2 /facebook/link
+
+This fragment shows the standard Facebook login button, prompting the user to
+link his account.
+
+=cut
+
+template '/facebook/link' => sub {
+    my ($plugin) = Jifty->find_plugin('Jifty::Plugin::Authentication::Facebook');
+    div {{ id is 'facebook_link' };
+        span { _("Login to Facebook now to link it with your current account!") };
+        a {{ href is $plugin->get_link_url };
+            img {{ src is 'http://static.ak.facebook.com/images/devsite/facebook_login.gif', border is '0' }};
+        };
+    };
+};
+
+
 1;


More information about the Jifty-commit mailing list