Skip to content

Instantly share code, notes, and snippets.

@melicarls
Last active May 11, 2016 16:59
Show Gist options
  • Save melicarls/520025d166df925fd7a4c25d7fdae7db to your computer and use it in GitHub Desktop.
Save melicarls/520025d166df925fd7a4c25d7fdae7db to your computer and use it in GitHub Desktop.
OmniAuth Lightning Talk

OmniAuth

What problem does the Gem solve?

This Gem solves the issue of user authentication by allowing users to register for and sign into an application using exisiting social media profiles. There is an enormous list of supported 'strategies' (types of 3rd party profile) [here] (https://github.com/intridea/omniauth/wiki/List-of-Strategies).

How do you use it in a Rails app?

OmniAuth acts as a go-between for your application and the strategy that will be providing the authentication. It's Rack Middleware, which means that it transmits information between your application and the strategy in a way that is independent from your application logic. As the docs say, it is designed as a "black box" into which you send your application data and receive information without making assumptions about or influencing your application's structure. Its flexible design is also intended to support several auth services in one application.

In order to use OmniAuth, you will need to modify the following 7 files:

Gemfile

Different strategies have their own gems, so you may want to to explore other (or several!) options.

gem 'omniauth-twitter'
config/initializers/omniauth.rb

You will need to create this file.

Rails.application.config.middleware.use OmniAuth::Builder do
   provider :twitter, 'CONSUMER_KEY', 'CONSUMER_SECRET' (replace these with the information that you get from your service)
end
routes.rb

Note that RailsCasts doesn't include the 'via' section of the code below. 'match' has been depreciated, so include it if you don't want your application to throw errors!

  match "/auth/:provider/callback" => "sessions#create", :via => [:get], :as => 'new_session'
  match "/signout" => "sessions#destroy", :as => :signout, :via => [:delete], :as => 'destroy_session'
sessions_controller.rb
def create
  auth = request.env["omniauth.auth"]
  user = User.find_by_provider_and_uid(auth["provider"], auth["uid"]) || User.create_with_omniauth(auth)
  session[:user_id] = user.id
  redirect_to root_url, :notice => "Signed in!"
end

def destroy
  session[:user_id] = nil
  redirect_to root_url, :notice => "Signed out!"
end
model/user.rb
def self.create_with_omniauth(auth)
  create! do |user|
    user.provider = auth["provider"]
    user.uid = auth["uid"]
    user.name = auth["info"]["name"]
  end
end
application_controller.rb
helper_method :current_user

private

def current_user
  @current_user ||= User.find(session[:user_id]) if session[:user_id]
end
application.html
<% if current_user %>
  Welcome <%= current_user.name %>!
  <%= link_to "Sign Out", signout_path %>
<% else %>
  <%= link_to "Sign in with Twitter", "/auth/twitter" %>
<% end %>

Restart your server and that's it! OmniAuth can also be used in conjuction with you app-specific username and password systems, like we did with bcrypt or using another gem like Devise. There's a great [RailsCast about that here] (http://railscasts.com/episodes/235-omniauth-part-1). Make sure you also watch [Part 2] (http://railscasts.com/episodes/236-omniauth-part-2) if you want to go that route.

Resources

@melicarls
Copy link
Author

screen shot 2016-05-10 at 4 46 01 pm

Use this callback URL for localhost testing. I didn't have time to test a production app but suspect that this will need to be changed to your hosted application location when you deploy.

@melicarls
Copy link
Author

screen shot 2016-05-10 at 4 46 35 pm

This is where you'll find the information that you need to add to your initializer file.

@melicarls
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment