Authlogic and Facebooker with Canvas App - Coherent Current_User

9 views
Skip to first unread message

rcwhite

unread,
Mar 3, 2010, 11:07:37 AM3/3/10
to facebooker
Hi Everyone,

I'm currently building a web application in Rails. I'm using Authlogic
to handle normal authentication. I'm using facebooker to handle
facebook connect on the external site and to authenticate users within
the facebook canvas application.

I'm having trouble building simple, coherent current_user
functionality. Currently I have a whole mess of different methods
getting/setting the current user, and I'd like to simplify this as
much as possible.

I'm using active_record_store for my sessions (defined in
environment.rb)
Here is my application Controller:


class ApplicationController < ActionController::Base

helper :all # include all helpers, all the time
protect_from_forgery # See
ActionController::RequestForgeryProtection for details

filter_parameter_logging :fb_sig_friends, :password, :password_confirmation

helper_method :facebook_session, :current_user_session, :current_user

before_filter :fb_setup

attr_accessor :current_user
helper_attr :current_user


#Before filter to decide if we should use facebook sessions to
set current user

def fb_setup
if request_comes_from_facebook?
ensure_authenticated_to_facebook
set_current_fb_user
end
end

############
# AuthLogic
############
def current_user_session
return @current_user_session if defined?
(@current_user_session)
@current_user_session = UserSession.find
end

def current_user
return @current_user if defined?(@current_user)
@current_user = current_user_session &&
current_user_session.user
end

##########
# Facebook
##########
def create_user
self.current_user = User.for(facebook_session.user.to_i,
facebook_session)
end

def set_current_fb_user
self.current_user = User.for(facebook_session.user.to_i,
facebook_session)
end

end


And in my User model, I have these methods to help finding/creating a
user from a facebook id:
def self.for(facebook_id,facebook_session=nil)
returning find_or_create_by_facebook_id(facebook_id) do |user|
unless facebook_session.nil?
user.store_session(facebook_session.session_key)
end
end
end


def store_session(session_key)
if self.session_key != session_key
update_attribute(:session_key,session_key)
end
end

def facebook_session
@facebook_session ||=
returning Facebooker::Session.create do |session|
session.secure_with!(session_key,facebook_id,1.day.from_now)
end
end

I need either self.current_user or @current_user set in
application_controller in a coherent way regardless of which session
is responsible for setting the current_user. I'm having trouble doing
this and the rest of my code base is suffering because of this. If
anyone has any advice, it would be much appreciated.

If you need any additional info, I'll be happy to provide it.

Thanks

David Sifry

unread,
Mar 3, 2010, 11:21:58 AM3/3/10
to faceb...@googlegroups.com
This is a use case I can really relate to! I'd love to work together with others to build out a nice skeleton Rails app that handled this kind of use case in a sane way! I too have something that sorta works, but is really hard to use and improve. It would sem that this is the kind of use case that anyone who is developing a Rails + Facebook Connect-enabled site would encounter, it would be a dream to have a simple way to make this happen...

Dave

> --
> You received this message because you are subscribed to the Google Groups "facebooker" group.
> To post to this group, send email to faceb...@googlegroups.com.
> To unsubscribe from this group, send email to facebooker+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/facebooker?hl=en.
>

--
David L. Sifry
415 846-0232 (Mobile)
Blog: http://www.sifry.com/alerts
Photos: http://flickr.com/photos/dsifry


rcwhite

unread,
Mar 3, 2010, 11:45:33 AM3/3/10
to facebooker
I'd definitely be willing to work with people to try and build a
skeleton app (especially because it's vital to my application). I'm
going to do a bit more research, then I'll set up a public github repo
and facebook app, and if people are interested they can help me build
out the skeleton. I'll notify this post when everything is ready.


Charlie

> > For more options, visit this group athttp://groups.google.com/group/facebooker?hl=en.

rcwhite

unread,
Mar 5, 2010, 3:40:27 PM3/5/10
to facebooker
Hi Everyone,

I got a skeleton application up and running.

The web application is hosted free at http://facebooker-authlogic-bridge.heroku.com/

The Facebook application is located at http://apps.facebook.com/fb-authlogic-bridge/

The code is hosted at Github here, http://github.com/fluidtickets/facebooker-authlogic-bridge

The app handles authentication from 3 sources: authlogic, facebook
connect, and facebook canvas apps.

you can use the current_user method (like normally used in Authlogic)
to get the current_user, no matter how they authenticated.

Let me know if there are any bugs, and hopefully this helps someone.

David Sifry

unread,
Mar 5, 2010, 3:54:19 PM3/5/10
to faceb...@googlegroups.com
Fantastic, I'm going to go give it a try...

Dave

> For more options, visit this group at http://groups.google.com/group/facebooker?hl=en.

David Sifry

unread,
Mar 9, 2010, 8:36:42 PM3/9/10
to faceb...@googlegroups.com
Hi! Thanks a lot for posting your skeleton app. I've been attempting
to get it running flawlessly over my tunneled connection.

I was wondering if you could tell me what you're using as application
settings in Facebook - e.g. the callbacks.

For example, what do you have in your Post-Authorize Callback URL and
Post-Remove Callback URL? Canvas Callback URL, Post-Authorize Redirect
URL, and Connect URL?

Many thanks, I'm sure it will help me get things debugged and working
on my site...

Dave

rcwhite

unread,
Mar 10, 2010, 8:54:02 AM3/10/10
to facebooker
David,

My Post-Authorize Callback URL and Post-Remove Callback URL are
blank.

My canvas callback url is http://facebooker-authlogic-bridge.heroku.com/,
which is the root of my web application.

Post-Authorize Redirect is blank.

Connect URL is also http://facebooker-authlogic-bridge.heroku.com/

Note that in Facebooker.yml neither the canvas page or the
callback_url have trailing slashes.

What errors are you seeing?

To be honest, I haven't tried this over a tunneled connection. If
you have more trouble, maybe try creating a free Heroku account, and
try to get that working first.

On Mar 9, 8:36 pm, David Sifry <dsi...@gmail.com> wrote:
> Hi! Thanks a lot for posting your skeleton app. I've been attempting  
> to get it running flawlessly over my tunneled connection.
>
> I was wondering if you could tell me what you're using as application  
> settings in Facebook - e.g. the callbacks.
>
> For example, what do you have in your Post-Authorize Callback URL and  
> Post-Remove Callback URL? Canvas Callback URL, Post-Authorize Redirect  
> URL, and Connect URL?
>
> Many thanks, I'm sure it will help me get things debugged and working  
> on my site...
>
> Dave
>
> On Mar 5, 2010, at 12:40 PM, rcwhite wrote:
>
>
>
>
>
> > Hi Everyone,
>
> > I got a skeleton application up and running.
>

> > The web application is hosted free athttp://facebooker-authlogic-bridge.heroku.com/
>
> > The Facebook application is located athttp://apps.facebook.com/fb-authlogic-bridge/
>
> > The code is hosted at Github here,http://github.com/fluidtickets/facebooker-authlogic-bridge

rcwhite

unread,
Mar 10, 2010, 8:56:18 AM3/10/10
to facebooker
Also,
On a more general note. the method current_user must be used
through out the application rather than the instance variable
@current_user. The method does the necessary legwork to ensure the
instance variable is set and returned correctly each invocation.

On Mar 9, 8:36 pm, David Sifry <dsi...@gmail.com> wrote:

> Hi! Thanks a lot for posting your skeleton app. I've been attempting  
> to get it running flawlessly over my tunneled connection.
>
> I was wondering if you could tell me what you're using as application  
> settings in Facebook - e.g. the callbacks.
>
> For example, what do you have in your Post-Authorize Callback URL and  
> Post-Remove Callback URL? Canvas Callback URL, Post-Authorize Redirect  
> URL, and Connect URL?
>
> Many thanks, I'm sure it will help me get things debugged and working  
> on my site...
>
> Dave
>
> On Mar 5, 2010, at 12:40 PM, rcwhite wrote:
>
>
>
>
>
> > Hi Everyone,
>
> > I got a skeleton application up and running.
>

> > The web application is hosted free athttp://facebooker-authlogic-bridge.heroku.com/
>
> > The Facebook application is located athttp://apps.facebook.com/fb-authlogic-bridge/
>
> > The code is hosted at Github here,http://github.com/fluidtickets/facebooker-authlogic-bridge

David Sifry

unread,
Mar 10, 2010, 2:59:58 PM3/10/10
to faceb...@googlegroups.com
Ah, making sure that there was no final slash in the callback_url in
the facebooker.yml was the key. If you're going to use a tunnel, this
is the way to specify it:

development:
api_key: XXXXX
secret_key: YYYYYYY
canvas_page_name: your_canvas_page_name
callback_url: http://www.yourdomain.com:3000
pretty_errors: true
set_asset_host_to_callback_url: true
tunnel:
public_host_username: your_username
public_host: the_tunnel_host
public_port: 3000
local_port: 3000
server_alive_interval: 0

Changing the callback_url from:
http://www.yourdomain.com:3000/
to
http://www.yourdomain.com:3000

made all the difference. Thanks!

Dave

> For more options, visit this group at http://groups.google.com/group/facebooker?hl=en

nitsujri

unread,
Mar 17, 2010, 6:43:41 PM3/17/10
to facebooker
Hey guys, I'm trying to learn how to write fbconnect for rails using
authlogic and fbooker, this is the simplest example that i've found,
but when I try to run it locally I get:

undefined method `find_or_create_by_facebook_id' for #<Class:
0xbf75f58>

Obviously the github version doesn't have that, so any thoughts into
why I'm going about this wrong?

Justin

On Mar 10, 12:59 pm, David Sifry <dsi...@gmail.com> wrote:
> Ah, making sure that there was no final slash in the callback_url in  
> the facebooker.yml was the key. If you're going to use a tunnel, this  
> is the way to specify it:
>
> development:
>    api_key: XXXXX
>    secret_key: YYYYYYY
>    canvas_page_name: your_canvas_page_name
>    callback_url:http://www.yourdomain.com:3000
>    pretty_errors: true
>    set_asset_host_to_callback_url: true
>    tunnel:
>      public_host_username: your_username
>      public_host: the_tunnel_host
>      public_port: 3000
>      local_port: 3000
>      server_alive_interval: 0
>
> Changing the callback_url from:http://www.yourdomain.com:3000/

> tohttp://www.yourdomain.com:3000


>
> made all the difference. Thanks!
>
> Dave
>
> On Mar 10, 2010, at 5:54 AM, rcwhite wrote:
>
>
>
> > David,
>
> >  My Post-Authorize Callback URL and Post-Remove Callback URL are
> > blank.
>

> >  My canvas callback url ishttp://facebooker-authlogic-bridge.heroku.com/


> > ,
> > which is the root of my web application.
>
> >  Post-Authorize Redirect is blank.
>

> >  Connect URL is alsohttp://facebooker-authlogic-bridge.heroku.com/

Mike Mangino

unread,
Mar 18, 2010, 8:17:13 AM3/18/10
to faceb...@googlegroups.com
Does the class you're calling that on have a facebook_id column? That's a Rails dynamic finder, not a facebook thing.

Mike

--
Mike Mangino
http://www.elevatedrails.com

rcwhite

unread,
Mar 18, 2010, 10:47:23 AM3/18/10
to facebooker
To be more specific, there is a migration in the project that adds the
facebook_id and session_key columns to the User table. Is your
database up to date?

> ...
>
> read more »

nitsujri

unread,
Mar 18, 2010, 12:37:48 PM3/18/10
to facebooker
Thanks guys!! Somehow my migration kept never including it, but I
wiped the sqlite3 and it all works.

Justin

> ...
>
> read more »

Jason FB

unread,
Mar 18, 2010, 1:32:35 PM3/18/10
to faceb...@googlegroups.com
Does anybody know how I can put a real simple "Contact Developer" link on my facebook application page?

I know there's one inserted by Facebook in the lower-right corner of my page (FBML), but I want it to be more prominent. I wouldn't mind using a link that worked just like that link -- I just want to embed it at the very top of my page.

Anyone know how to do this?

-Jason

Jason FB

unread,
Mar 18, 2010, 1:37:38 PM3/18/10
to faceb...@googlegroups.com
[wrong subject link in my last post]

rcwhite

unread,
Mar 18, 2010, 2:56:47 PM3/18/10
to facebooker
Reply all
Reply to author
Forward
0 new messages