/ / Omniauthがログイン時にOAuthトークンシークレットを更新しない - ruby​​-on-rails-3、twitter、omniauth

Omniauthがログイン時にOAuthトークンシークレットを更新しない - ruby​​-on-rails-3、twitter、omniauth

私はOmniauthを使ってTwitterやFacebookでユーザーを認証し、トピックに関する "標準"チュートリアルRyan Bates "スクリーンキャスト私はAuthlogicを使用していますが、Deviseでは使用していません)。

私はTwitterを使ってログインできますが、処理できませんTwitterのアクセストークンの秘密がTwitterの終わりに変更されたが、私のアプリケーションの終わりでは更新されていないため、認証された要求が戻ってくる。私は認証を削除しようとしましたが、何らかの理由で古いものを保存しています。

authentications_controller.rb

def create
omniauth = request.env["omniauth.auth"]
authentication = Authentication.find_by_provider_and_uid(omniauth["provider"], omniauth["uid"])

if authentication
# User is already registered with application
flash[:notice] = "Signed in successfully."
sign_in_and_redirect(authentication.user)
elsif current_user
# User is signed in but has not already authenticated with this social network
current_user.authentications.create!(:provider => omniauth["provider"], :uid => omniauth["uid"], :token => (omniauth["credentials"]["token"] rescue nil), :secret => (omniauth["credentials"]["secret"] rescue nil))
current_user.apply_omniauth(omniauth)
current_user.save

flash[:notice] = "Authentication successful."
redirect_to root_url
else
# User is new to this application
@user = User.new
@user.apply_omniauth(omniauth)

if @user.save
flash[:notice] = "User created and signed in successfully."
sign_in_and_redirect(@user)
else
session[:omniauth] = omniauth.except("extra")
redirect_to new_user_path
end
end
end

user.rb

def apply_omniauth(omniauth)
self.email = "foo@example.com"
self.login = omniauth["user_info"]["nickname"] if login.blank?
authentications.build(:provider => omniauth["provider"], :uid => omniauth["uid"], :token => omniauth["credentials"]["token"], :secret => omniauth["credentials"]["secret"])
end

何か案は? Rails 3.0.6とRuby 1.8.7

回答:

回答№1は2

スティーブ、あなたは次のことを試すことができます:

if authentication
# Make sure we have the latest authentication token for user
if omniauth["credentials"]["token"] && omniauth["credentials"]["token"] != authentication.token
# puts "Found Invalid token"
authentication.update_attribute(:token, omniauth["credentials"]["token"])
end
flash[:notice] = "Signed in successfully"
sign_in_and_redirect(:user, authentication.user)
elsif ...

これは、すでに登録されたユーザーがログインしようとするたびにトークンの不一致が発生するたびに、ユーザーのアクセストークンを基本的に更新する必要があります。