Ruby on Rails: Authlogic Facebook-Integration: Link Konten mit Benutzereingabe statt automatisch unter Verwendung von E-Mail-Adresse

StackOverflow https://stackoverflow.com/questions/4512277

Frage

Ich entwickle einen Web-app in Ruby on Rails. Ich verwende authlogic Authentifizierung zu tun. Meine Website verfügt über eine eigene Logins, aber ich hoffe, auch Facebook verwenden. Ich habe versucht, aus authlogic_facebook_connect (mit Facebooker). Nach viel Schluckauf (die Dokumentation nicht wirklich vollständig gehalten wurde), Habe ich authlogic_facebook_connect an die Arbeit und es ist OK. Der Standard „connect“ Verhalten funktioniert perfekt, wenn ich mit den Benutzern konfrontiert bin, die noch nie von Website verwendet haben, aber es ergibt sich eine Menge von doppelten Anmeldungen für Leute, die verschiedene E-Mail-Adressen für Facebook und für meine Website verwenden. Hier ist, was ich will:

Wenn der Benutzer die Facebook „Connect“ Taste trifft (und, nachdem sie durch die Facebook-Auth Schritt des Klickens gehen ‚Zulassen‘), möchte ich ein Feld um den Benutzer Pop-up zu fragen, ob sie eine verbinden möchten vorbestehenden Konto auf meiner Seite oder, wenn sie für sie ein Konto automatisch generiert haben will.

Wenn sie wollen, dass es für sie automatisch generiert, wir sind gut und wir gehen Sie wie normal, aber wenn - auf der anderen Seite - sie wollen ihre Facebook-Konto auf ein Konto auf meine Seite verlinken, ich möchte, dass sie tatsächlich in ihren lokalen Anmeldeinformationen und finden Sie das richtige Konto einzugeben. Mit anderen Worten, ich nicht möchte, dass meine Lösung automatisch herauszufinden, welche aussehen wie das richtige Konto, ich möchte der Benutzer dies zu tun.

Gibt es einen Edelstein / plugin / quick Hack, der mir erlaubt, diese entweder zu ziehen aus mit authlogic_facebook_connect oder OAuth oder etwas anderes?

- David

War es hilfreich?

Lösung

Someone else may be able to point you at the perfect gem for this, but I can tell you that I've worked on a similar problem and it wasn't much work to roll our own, based on the oauth2 gem.

Here's a sketch of the code/flow I use.

1) User clicks on 'Connect to Facebook' and this sends you to an action like this

def to_facebook
    options = {
      :redirect_uri => facebook_callback_url,
      :scope => "email,publish_stream" # whatever you want to do
    }
    client = OAuth2::Client.new(FACEBOOK_API_KEY, FACEBOOK_API_SECRET, :site => FACEBOOK_API_SITE)

    redirect_to client.web_server.authorize_url(options)
end

2) User goes over to facebook and gives you all the access you want, then facebook calls the callback you specified facebook_callback_url which should take you to an action like this:

def facebook_callback
    client = OAuth2::Client.new(FACEBOOK_API_KEY, FACEBOOK_API_SECRET, :site => FACEBOOK_API_SITE)
    access_token = client.web_server.get_access_token(params[:code], :redirect_uri => facebook_callback_url)

    do_my_custom_user_association(access_token)
end

Then you can specify whatever you want in do_my_custom_user_association, you should have a current_user available from authlogic if someone is logged in, so you can redirect to a flow that lets the logged in user select if they want to merge into their current account or a different one. If there's no current user, you can send them to a create account flow, with some facebook data attached.

Note that this is just a sketch, there are error cases to handle (e.g. facebook_callback will be hit with the param error_reason if the get_acccess_token fails) and I'm not recommending you do all the oauth2 interaction right in your controller, but the basic idea is there.

See http://developers.facebook.com/docs/authentication/ if any of the oauth2 interactions don't make sense.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top