Set up omniauth Github authentication for different environments (dev, prod) in a Rails app

by trivektor

Recently, I’ve been developing a personal project that makes use of the Github oauth api. For this purpose, I use the devise gem with support for omniauth. I figured I’d need two different sets of credentials for Github authentication since I want to develop locally and test my app on production. It turned out pretty easy.

config.omniauth :github, ENV['GITHUB_ID'], ENV['GITHUB_SECRET'], :scope => 'user,public_repo'

Ruby can read environment variables on your computer through the ENV hash. If you’re on Mac and you the bash shell, all you have to do is to open ~/.bash_profile and add (don’t forget to restart the shell for these variables to take effect)

export GITHUB_ID="Your github app id"
export GITHUB_SECRET="Your github app secret"

(by the way, you can register your app here

To push these values to Heroku (if it’s the platform you deploy to) do

heroku config:add GITHUB_ID=[Your github app id] GITHUB_SECRET=[Your github app secret]

The technique also works for Twitter and Facebook authentication strategy.