Skip to content

Instantly share code, notes, and snippets.

@jose8a
Last active August 29, 2015 14:12
Show Gist options
  • Save jose8a/1b00dbe1071e4118ab2a to your computer and use it in GitHub Desktop.
Save jose8a/1b00dbe1071e4118ab2a to your computer and use it in GitHub Desktop.
Adding Devise + OmniAuth to Rails 4 App
### This Guide is summary of steps outlined in the guide published by
### DigitalOcean here: https://www.digitalocean.com/community/tutorials/how-to-configure-devise-and-omniauth-for-your-rails-application
### TODO: automate this guide into a ruby script
### TODO: required auth tokens to be read from a local 'keys' folder w/individual
### PROVIDER (Twitter, FB, GH, Goog, etc.) token json files
X ----- [1] Add to Gemfile
gem 'therubyracer'
gem 'devise'
gem 'omniauth'
gem 'omniauth-twitter'
$> bundle install
X ----- [2] Set Up Devise
### Generate the necessary models needed by Devise
rails generate devise:install
rails generate devise User
rake db:migrate
### Add user authorization to all pages by adding to
### Add to $APPROOT/app/controllers/application_controller.rb :
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
before_action :authenticate_user! #===> added this line
end
X ----- [3] Update User Model to Support OmniAuth
rails g migration AddColumnsToUsers provider uid
rake db:migrate
X ----- [4] Get the Client ID and Client Secret from the OAuth Service Provider
### You will be asked for a callback URL during the registration process.### There is a separate callback URL for each provider. Here are the
### callback URLs for a few popular service providers:
* Facebook: http://localhost:3000/users/auth/facebook/callback
* Amazon: http://localhost:3000/users/auth/amazon/callback
* Twitter: http://localhost:3000/users/auth/twitter/callback
* Google: http://localhost:3000/users/auth/google/callback
X ----- [5] Update the Devise Initializer
### Now that you have the App credentials (CLIENT_ID and CLIENT_SECRET),
### we need to configure Devise with these credentials.
### ==> edit $APPROOT/config/initializers/devise.rb (add App credentials)
### After editing, your file looks something like this (minus comments):
Devise.setup do |config|
#Replace example.com with your own domain name
config.mailer_sender = '[email protected]'
require 'devise/orm/active_record'
config.case_insensitive_keys = [ :email ]
config.strip_whitespace_keys = [ :email ]
config.skip_session_storage = [:http_auth]
config.stretches = Rails.env.test? ? 1 : 10
config.reconfirmable = true
config.expire_all_remember_me_on_sign_out = true
config.password_length = 8..128
config.reset_password_within = 6.hours
config.sign_out_via = :delete
#Add your ID and secret here
#ID first, secret second
config.omniauth :digitalocean, "db381dc9990be7e3bc42503d0", "5b0824c2722b65d29965f1a1df"
end
X ----- [6] Update the USER Model
### ==> Do two things here:
### ==> (1) adding three items to the existing list (:omniauthable,
:omniauth_providers => [:digitalocean], and don't forget the extra comma!)
### ==> (2) create a new method named from_omniauth to extract the
information that is available after the authentication.
### ==> the model USER should now look as follows:
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:omniauthable, :omniauth_providers => [:digitalocean]
def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
user.provider = auth.provider
user.uid = auth.uid
user.email = auth.info.email
user.password = Devise.friendly_token[0,20]
end
end
end
----- [7] Add a Controller to Handle the Callback URLs
### ==> edit ~/rails_apps/myapp/config/routes.rb and,
### ==> update the devise_for line to specify the name of the
controller that will be handling the callbacks
### ==> $APPROOT/config/Routes.rb should have the following:
Rails.application.routes.draw do
devise_for :users, :controllers => { :omniauth_callbacks => "callbacks" } ### ==> add this line
resources :products
root 'products#index'
end
### ==> Then, create a new file:
$APPROOT/app/controllers/callbacks_controller.rb
class CallbacksController < Devise::OmniauthCallbacksController
def digitalocean
@user = User.from_omniauth(request.env["omniauth.auth"])
sign_in_and_redirect @user
end
end
----- [8] Add Login/Logout links/buttons to the application.html.erb template
### ==>
<% if user_signed_in? %>
<li>
<%= link_to('Logout', destroy_user_session_path, :method => :delete) %>
</li>
<% else %>
<li>
<%= link_to('Login', new_user_session_path) %>
</li>
<% end %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment