哪一个更好 ? Omniauth或Omniauth +设计用于Twitter验证?

时间:2013-05-20 06:29:30

标签: ruby-on-rails

如果我使用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 + devise for 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 + Devise哪个更好?

1 个答案:

答案 0 :(得分:2)

Devise会为您提供一些现成的帮助函数,例如current_useruser_signed_in?和控制器过滤器,例如before :authenticate_user!

虽然它们是我们自己实施的基础,但仅在omniauth-twitter时,您必须自己实施。

如果您不打算进行非常复杂的用户身份验证,只想进行简单的基于Twitter的身份验证,则可以单独使用omniauth-twitter。默认情况下,Devise将打开您的mannual(基于表单)注册申请,您可能不需要。