如何使用omniauth gem刷新令牌? - Rails 4.2

时间:2015-08-07 17:45:28

标签: ruby-on-rails ruby ruby-on-rails-3 omniauth

到目前为止,我已使用omniauth-google-oauth2 gem设法使用google plus登录用户。

就我而言,您会获得一个在某个时间到期的访问令牌。如果用户在一周后在我的网站上使用google plus登录,我该如何从谷歌API刷新该令牌,让我们每周说一次?

下面是我在sessioncontroller.rb中的代码

def google_oauth2
    auth_hash = request.env['omniauth.auth']

    @identity = Identity.find_by_provider_and_uid(auth_hash["provider"], auth_hash["uid"])
    if @identity
      log_in @identity.user
      flash[:success] = "Welcome, #{@identity.user.name}!"
    else
      random_password = SecureRandom.urlsafe_base64(n=6)
      #@user = create_with_omniauth(auth_hash)
      @user = User.new(
        name:auth_hash['info']['name'],
        email:auth_hash['info']['email'],
        password:random_password,
        password_confirmation:random_password,
        activated:true
      )
      @user.identities.build(
        uid: auth_hash['uid'],
        provider: auth_hash['provider'],
        image_url: auth_hash['info']['image'],
        oauth_token: auth_hash['credentials']['token'],
        oauth_refresh_token: auth_hash['credentials']['refresh_token'],
        oauth_expires_at: auth_hash['credentials']['expires_at']
      )
      if @user.save!
        log_in @user
        flash[:success] = "Hello, #{@user.name}! An account has been created for you!"
      else
        flash[:warning] = "There was an error while trying to authenticate you..."
      end
    end
    redirect_to root_path
  end

某些属性保存在用户表中,而某些属性表中的某些属性具有has_many标识,属于用户模型,因为将来我想添加多个提供程序。

0 个答案:

没有答案