Use Omniauth/Devise with different Facebook apps (e.g. one for every locale)

To use different Facebook apps for authentication with Devise and OmniAuth:

  1. Add a setup method to Omniauth that dynamically sets the client id and secret:
    # config/initializers/devise.rb
    config.omniauth :facebook, nil, nil, setup: true
  1. Setup a route to your setup method:
    # config/routes.rb
    match '/auth/:provider/setup' => 'sessions#setup'
  1. Use the setup method to set the id and secret options in the request object:
    def setup
      # in this example facebook keys for different site domains are stored in Config   
      request.env['omniauth.strategy'].options[:client_id] = Config[domain]['appKey']
      request.env['omniauth.strategy'].options[:client_secret] = Config[domain]['appSecret']
      render :text => "Omniauth setup phase.", :status => 404
    end

Caveat: Facebook does a lot of caching even for apps in development mode. Logging in might not immediately work – if in doubt, create a fresh facebook app and check if you can login with its id and secret.

Rin About 10 years ago