加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails-3 – Omniauth在登录时不更新OAuth令牌密钥

发布时间:2020-12-17 07:08:44 所属栏目:百科 来源:网络整理
导读:我正在使用Omniauth通过Twitter和Facebook对用户进行身份验证,通过主题的“标准”教程( Ryan Bates’ screencast,虽然我使用的是Authlogic,而不是Devise). 我可以使用Twitter登录,但无法处理经过身份验证的请求,因为我的Twitter访问令牌密码已在Twitter端更
我正在使用Omniauth通过Twitter和Facebook对用户进行身份验证,通过主题的“标准”教程( Ryan Bates’ screencast,虽然我使用的是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'],:token => omniauth['credentials']['token'],:secret => omniauth['credentials']['secret'])
end

有任何想法吗? Rails 3.0.6和Ruby 1.8.7

解决方法

史蒂夫,你可以尝试以下方法:

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

每次已注册的用户尝试登录以及发生令牌不匹配时,这基本上应该更新用户的访问令牌.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读