剛剛做到了這一點,但從未見過端到端的解決方案。
此地址點3. 1 & 2很容易實現設計和記錄在別處。
不難將FB認證添加到您的網絡應用程序中,說明在github上爲omniauth和omniauth-facebook。
我相信如果你想這樣做,以下是獨立的,沒有做omniauth-facebook集成。這與其他方法類似。我的想法是試圖儘可能使用設計模型。
您將需要fb_graph寶石。
在移動客戶端上,您可以使用FB進行相應的身份驗證,並將返回的訪問令牌放入您的http請求的頭部。我使用頭fb_access_token。 就像基本身份驗證一樣,您需要通過SSL發送該消息以避免嗅探令牌。使用頭允許我在不更改請求的情況下交換基本身份驗證和FB身份驗證,但是如果您願意,可以使用參數。
該解決方案實現了基於設計Authenticatable監管策略的監獄戰略。這裏的區別在於這個策略使用了一個名爲fb_access_token的HTTP頭,其中包含使用移動應用程序檢索到的Facebook訪問令牌字符串。
一旦你知道這一點,代碼是非常簡單的。
在文件中,在config/initializers目錄中添加以下內容。我碰巧打電話給我的fb_api_strategy。RB:
# authentication strategy to support API authentication over the webservice
# via facebook
require 'devise/strategies/database_authenticatable'
require 'fb_graph'
require 'warden'
module Devise
module Strategies
class FbMobileDatabaseAuthenticatable < Authenticatable
def valid?
# if we have headers with the facebook access key
!!request.headers["fb_access_token"]
end
def authenticate!
token = request.headers["fb_access_token"]
fbuser = FbGraph::User.me(token)
fbuser = fbuser.fetch
user = User.find_for_facebook_mobile_client(fbuser.email)
# this either creates a new user for the valid FB account, or attaches
# this session to an existing user that has the same email as the FB account
if !!user && validate(user) { true }
user.after_database_authentication
success!(user)
elsif !halted? || !user
fail(:invalid)
end
end
end
end
end
Warden::Strategies.add(:fb_database_authenticatable,
Devise::Strategies::FbMobileDatabaseAuthenticatable)
在配置/初始化,添加以下devise.rb:
config.warden do |manager|
manager.default_strategies(:scope => :user).unshift :fb_database_authenticatable
end
爲了讓您無論是創建一個用戶或發現基礎上,FB電子郵件的現有用戶,添加下面給您的用戶模型:
def self.find_for_facebook_mobile_client(fb_email)
if user = User.where(:email => fb_email).first
user
else
User.create!(:email => fb_email, :password => Devise.friendly_token[0,20])
end
end
我不認爲fb_database_authenticatable是一個準確的名字,但我會離開,作爲一個練習留給讀者。另一個運動是高速緩存/存儲FB訪問令牌,也許避免RT與每個呼叫到FB。你應該注意的是,如果你在兩邊,我相信大部分人會不願意做FB驗證從移動應用和Rails應用程序的訪問令牌會有所不同。這可能會影響您的緩存方案。
我認爲,做它 - 快樂編碼。
一些需要注意的:基本權限使用FB帳戶不要提供電子郵件地址。你需要明確地詢問用戶這個權限。 – beeudoublez
好一點@beeudoublez,這正是我在我的應用程序一樣。 – Matt