#939·omniauth

What's the best way to find a OmniAuth strategy by :name?

Author: aguynamedbenCreated Aug 10, 2018Updated Mar 15, 2024

I want to dynamically look up a specific OmniAuth strategy by its :name. For example, the Google strategy has the name of 'google_oauth2' (code).

OmniAuth.strategies is a list of classes:

[
    [ 0] OmniAuth::Strategies::OAuth2 < Object,
    [ 1] OmniAuth::Strategies::Asana < OmniAuth::Strategies::OAuth2,
    [ 2] OmniAuth::Strategies::Bitbucket < OmniAuth::Strategies::OAuth2,
...

It doesn't seem there's a class property for :name, so to find a strategy by name I have to create instances:

rb
strategy_klass = OmniAuth.strategies.find do |klass|
  instance = klass.new(nil)
  instance.name == 'google_oauth2'
end
# strategy_klass is now OmniAuth::Strategies::GoogleOauth2 < OmniAuth::Strategies::OAuth2

Is there a more recommended way to find a strategy by :name? I looked through the docs and code and didn't see one.

Thanks for the time you put into OmniAuth, it's amazing.