Ruby on Rails 4, Salesforce OAuth authentication and Restforce

Ruby on Rails 4, Salesforce OAuth authentication and Restforce

Posted on April 7, 2014

EditI’ve updated some parts to work with the latest version of Rails at the moment (4.1.7).The code of this tutorial is here

The objective

In this post I’m going to explain how to add Salesforce OAuth authentication to a Rails application.
Later I’ll be explaining how to use restforce gem to access Salesforce.com data on behalf of the authenticated user.

Add a helper method to interact with current_user

Edit /app/controllers/application_controller.rb, this helper will be available in your app so we can know current user data.

classApplicationController<ActionController::Base# Prevent CSRF attacks by raising an exception.# For APIs, you may want to use :null_session instead.protect_from_forgerywith: :exceptionprivatedefcurrent_user@current_user||=User.find(session[:user_id])ifsession[:user_id]endhelper_method:current_userend

Tutorial Code

The code of this Tutorial can be found here.
Salesforce Connected app “Consumer Key” and “Consumer Secret” must be set on config/application.rb .
Additional interaction with Restforce can be seen on controllers/home_controller.rb.