2012-03-19 1 views
2

を考案:私は、ツイッターを使って署名を実装するためにこれらのrailscastsを参照omniauthと工夫していますツイッター、Omniauthを使用してサインインを実装すると

  1. http://railscasts.com/episodes/235-omniauth-part-1?view=asciicast
  2. http://railscasts.com/episodes/236-omniauth-part-2?view=asciicast

シナリオ: twitterのユーザーが私のアプリに来る。ログインするにはTwitterリンクをクリックし、/ auth/twitterに移動します。彼は私のアプリをTwitterサイトで許可し、私のアプリにリダイレクトされます。彼は私のサイトに自分の電子メールIDを入力します。それは必須です。彼はサイトを使い、私のアプリからサインアウトします。彼は再びログインし直したい。彼は再びTwitterのサインインアイコンをクリックします。 私の問題:私は、彼はTwitterのサイト上で、再び私のアプリを承認する必要がないだろうと予想。しかし、そうではありません。今回も、twitterはこのユーザーに認証を依頼しています。

私のコードは正確にrailscastsを次の:私は戻って、ユーザーが認証するために頼まれていないことを確認するために行う必要がある何

class User < ActiveRecord::Base 
    devise :database_authenticatable, :registerable, 
     :recoverable, :rememberable, :trackable, :validatable 

    # Setup accessible (or protected) attributes for your model 
    attr_accessible :email, :password, :password_confirmation, :remember_me 
    has_many :authentications 

    def apply_omniauth(omniauth) 
     authentications.build(:provider => omniauth['provider'], :uid => omniauth['uid']) 
    end 

    def password_required? 
     (authentications.empty? || !password.blank?) && super 
    end 
end 

:工夫から

<routes.rb> 
match '/auth/:provider/callback' => 'authentications#create' 


class AuthenticationsController < ApplicationController 
... 
def create 
    omniauth = request.env["omniauth.auth"] 

    authentication = Authentication.find_by_provider_and_uid(omniauth['provider'], omniauth['uid']) 

    if authentication 
     flash[:notice] = "Signed in successfully." 
     sign_in_and_redirect(:user, authentication.user) 
    elsif current_user 
     current_user.authentications.create(:provider => omniauth['provider'], :uid => omniauth['uid']) 
     flash[:notice] = "Authentication successful." 
     redirect_to authentications_url 
    else 
     user = User.new 
     user.apply_omniauth(omniauth) 
     if user.save 
     flash[:notice] = "Signed in successfully." 
     sign_in_and_redirect(:user, user) 
     else 
     session[:omniauth] = omniauth.except('extra') 
     redirect_to new_user_registration_url 
     end 
    end 
    end 
... 
end 

Userモデルがように変更されます毎回私のアプリ/彼は/ auth/twitterにリダイレクトされますか?

+0

そして、私のomniauth初期化子は、次のようになります。さえずり、 *******************:「Rails.application.config.middleware.use OmniAuth :: Builderは プロバイダを行います****、 *********************** 終了」 –

答えて

0

あなたが続く場合ライアンベイツ約工夫し、ユーザーモデルは、ユーザと認証の間に関連を持っている必要がありomniauthキャストします。 ':プロバイダ/コールバック/ AUTH /' => '認証番号が作成' にhas_many:

試合:

はあなたのroutes.rbをファイルには、ressourceを必要と

認証

Userモデル

はこのようなものが必要

devise_for:ユーザー:コントローラ=> {:セッション=> 'セッション':登録=> '登録'}

リソース:認証

希望すると、これが役立ちます。

敬具 月

関連する問題