简体   繁体   中英

Change Twitter provider after initialization with Omniauth gem

I'm using Omniauth to authenticate at Twitter, but I have 2 apps registred with diferent names that I want to use depeding on the current locale(session scope).

So I need to change the provider key and secret defined at omniauth.rb file right before user calls auth/twitter( I was thinking to do a before_filter but auth/twitter is an external link to twitter and not a regular action) or a way to config Omniauth to define providers by locale instead of define for the entire application scope.

So how can I do that ? Any idea?

What you need to do is set setup to true in omniauth builder

  Rails.application.config.middleware.use OmniAuth::Builder do
    provider :twitter , :setup => true
  end

Then set add following to your routes.rb file to define which route will be called for setup

  get '/people/auth/twitter/setup' => 'sessions#twitter_setup' #needed for devise setup phase hook to work

After so just set the omniauth strategy in the session controller that meet the route for setup

def twitter_setup
  request.env['omniauth.strategy'].options[:consumer_key] = YOUR_DYNAMIC_KEY
    request.env['omniauth.strategy'].options[:consumer_secret] = YOUR_DYNAMIC_SECRET_KEY
    render :plain => "Setup complete.", :status => 404
end

this will enable you to load the apps you need

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM