Ask A Question

Notifications

You’re not receiving notifications from this thread.

How to Refresh the OmniAuth AuthHash Discussion

Nice tutorial

Reply

This is a service that I wrote to decouple from the models into a service. this will only work with OAuth-derived strategies.

just call OmniAuthService.refresh!(user_omniauth_provider_record)

force refresh for tokens that have not expired yet

OmniAuthService.refresh!(user_omniauth_provider_record, force: true)

will return true if refreshed and false if not expired or not refreshed

class OmniAuthService
  attr_reader :provider, :name, :force

  # @param [users-provider-class] provider the users provider record
  def initialize(provider, force: false)
    @provider      = provider
    @name        ||= provider.provider.to_sym
    @force         = force
    @token_options = []
    @refreshed     = false
  end

  delegate :expired?,      to: :provider
  delegate :access_token,  to: :provider
  delegate :refresh_token, to: :provider

  # do not refresh unless expired token
  def refresh
    return unless force || expired?

    refresh_provider_tokens
  end

  def refreshed?
    @refreshed
  end

  class << self
    def refresh!(provider, force: false)
      o = new(provider, force:)
      o.refresh
      o.refreshed?
    end
  end

  private
  def refresh_provider_tokens
    new_token = current_token.refresh!
    return if new_token.blank?

    provider.update(
      access_token:  new_token.token,
      expires_at:    Time.zone.at(new_token.expires_at),
      refresh_token: new_token.refresh_token
    )
    @refreshed = true
  end

  def strategy
    Devise.omniauth_configs[name].strategy_class.new(
      Devise.omniauth_configs[name].strategy.app,
      *Devise.omniauth_configs[name].args
    )
  end

  def current_token
    puts token_options
    OAuth2::AccessToken.new(
      strategy.client,
      access_token,
      **token_options
    )
  end

  def token_options
    options = {}
    options.merge({ refresh_token: }) if refresh_token
  end
end


note the strategy method. no need to use const_get. just call the Devise initialized strategy

Reply

Your solution is so useful. Thank you for your sharing

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 87,563+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.