Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
aguynamedben opened this issue Aug 10, 2018 · 3 comments
Open

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

aguynamedben opened this issue Aug 10, 2018 · 3 comments

Comments

@aguynamedben
Copy link

aguynamedben commented Aug 10, 2018

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:

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

@aguynamedben
Copy link
Author

I found this https://github.com/omniauth/omniauth/blob/master/lib/omniauth/builder.rb#L52

middleware = OmniAuth::Strategies.const_get(OmniAuth::Utils.camelize('google_oauth2').to_s)
# returns OmniAuth::Strategies::GoogleOauth2

I guess now I want to automatically create an instance of the provider with the configuration given in my Rails config/initializers/omniauth.rb

  provider :google_oauth2, ENV["GOOGLE_OAUTH_CLIENT_ID"], ENV["GOOGLE_OAUTH_CLIENT_SECRET"],
           scope: "(...scopes...)"

Are there any ways to get the pre-configured instance of a strategy?

@kreintjes
Copy link

Are there any ways to get the pre-configured instance of a strategy?

Same question over here. @aguynamedben did you find a solution for this?

@aguynamedben
Copy link
Author

@kreintjes I don't recall, too much time passed!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants