2011-09-28 60 views
4

我使用的是Omniauth,通過關於主題的「標準」教程(Ryan Bates' screencast,儘管我使用的是Authlogic,而不是Devise)來使用Twitter和Facebook對用戶進行身份驗證。Omniauth沒有在登錄時更新OAuth令牌密碼

我可以使用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 = "[email protected]" 
    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 

任何想法?導軌3.0.6和Ruby 1.8.7

回答

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 ... 

這應該每一個已經註冊的用戶嘗試登錄的時間基本更新用戶的訪問令牌當令牌不匹配發生。