2013-05-20 78 views
3

如果我使用omniauth Twitter的驗證。對於下面的代碼是哪一個更好? Omniauth或Omniauth +設計爲twitter驗證?

def create 
    user = User.from_omniauth(env["omniauth.auth"]) 
    session[:user_id] = user.id 
    redirect_to root_url, notice: "Successfully signed in" 
end 

,但如果我使用omniauth +設計Twitter的verificaiton。對於下面的代碼是

def twitter 
    omni= request.env["omniauth.auth"] 
authentication = Authentication.find_by_provider_and_uid(omni['provider'],omni['uid']) 
if authentication 
    flash[:notice]="Logged in successfully" 
    sign_in_and_redirect User.find(authentication.user_id) 
elsif current_user 
    token=omni['credentials'].token 
    token_secret=omni['credentials'].secret 
    current_user.authentications.create! (:provider=>omni['provider'],:uid=>omni['uid'],:token=>token,:token_secret=>token_secret) 
    flash[:notice]="Authentication successful." 
    sign_in_and_redirect current_user 
else 
    user=User.new 
    user.apply_omniauth(omni) 
     if user.save 
     flash[:notice]="Logged in." 
     sign_in_and_redirect User.find(user.id) 
    else 
     session[:omniauth]=omni.except('extra') 
     redirect_to new_user_registration_path 
    end 
end 
end 

哪一個更好的Omniauth或Omniauth +設計?

回答

2

Devise會給你一定的現成的輔助功能,如current_useruser_signed_in?和控制過濾器像before :authenticate_user!

雖然他們是非常基本的我們自己實現,但是當單獨使用omniauth-twitter時,您將不得不自行實施。

如果您不打算擁有非常複雜的用戶身份驗證,並且只需要簡單的基於Twitter的身份驗證,則可以單獨使用omniauth-twitter。默認情況下,Devise會打開你的手動申請(基於表單的註冊),你可能不需要。

+0

感謝@kiddorails獲取詳細的答案。基本上我正在開發一個社交網絡。我將使用我自己的註冊表單以及Twitter,Facebook和谷歌身份驗證。我正在尋找更快的東西,哪一個會更好呢.omniauth or omniauth + Twitter?(你會用哪一個? – sp1rs

+0

@ user1990200好的,好吧,我有這兩個應用程序;在我提供了Facebook,Twitter和mannual註冊;爲此我使用了'omniauthable'策略Devise,在其他應用程序中,我只需要Facebook登錄機制,所以我繼續使用omniauth-facebook。 Devise + Omniauth。關於實施的細節 - https://github.com/plataformatec/devise/wiki/OmniAuth:-Overview – kiddorails

+0

thx @kiddorails – sp1rs