SocialLogin
This gem helps you to login to different social networks (Academic project). This is a basic configuration gem that use omniauth to make the authentication against social networks such as twitter or facebook. Some code generated by the gem and such other configuration where taked for railscast.com (Thanks Ryan!). You can improve and compleate this generated code with the omniauth episodes in http://railstcasts.com.
To use this gem, I assume that you have devise correctly configured. If not, I suggest you take a look at devise configuration.
Installation
Add this line to your application's Gemfile:
gem 'social_login'
And then execute:
$ bundle
Or install it yourself as:
$ gem install social_login
Usage
After install the gem, you have to run the two generators that the gem provides.
First run the basic install generator:
$ rails g social_login:install
Omniauth basic configuration
This generator will create the omniauth.rb
file in the config/initializers
directory. This file seems like this:
Rails.application.config.middleware.use OmniAuth::Builder do
provider :twitter, 'KEY', 'SECRET'
provider :facebook, 'KEY', 'SECRET', { :scope => 'email, offline_access, user_photos, publish_stream', :client_options => { :ssl => { :ca_path => "/etc/ssl/certs" } } }
provider :linkedin, 'KEY', 'SECRET'
end
There is a basic configuration when you have to put your social networks credentials. So, first you have to create a twitter application for instance (http://dev.twitter.com). Here you can add several providers, whatever you want, but you have to add their specific omniauth gem as well.
After this generation, this generator will create the callback route that you need to handle when the authentication against the social network is successful. This will add the below route to your config/routes.rb
file:
#match '/auth/:provider/callback' => 'authentications#create'
This is just a comment. Keep in mind that the AuthenticationsController
is just an example here. To make this works at this point, you have to create your "authentications" controller and uncomment this line. This is an example (remember to use the same attributes in your controller):
rails g rails g scaffold authentication user_id:integer provider:string uid:string
After this, migrate your data base (rake db:migrate
) and add the relationships to your user controller (if this is what you called your registration model) has_many :authentications
and, in your "authentications" controller belongs_to :user
This generator also add the default gem dependencies to your Gemfile
, so then, you have to run your bundle command.
gem 'omniauth-twitter'
gem 'omniauth-facebook'
gem 'omniauth-linkedin'
Later:
$ bundle install
With this, you shall have your omniauth basic configuration done.
Authentication configuration
This generator will create an example template create_authnetication.rb
in your public
directory that contains the basic code when an authentications it's going to be created.
This code asume that your controller name is authentications. You have to move this chunck of code to the sample to your controller create authentication.
Finally, this generator will generate too a partial and three social networs images taked from https://github.com/intridea/authbuttons.
You will found into your app/assests/images
directory the next three files:
twitter_64.png
facebook_64.png
linkedin_64.png
If you want to add a new social network, you have to add the image too.
In the public
directory of your rails application, you will find the partial called '_auth.html.erb'. Copy and paste this partial and put it into your authentication controller, then you can render this partial whenever you want in your application, something like this:
<%= render "athentications/auth" %>
If you want to add new social networks, you have to modify this file adding the new links.
With this simple steps, you now have a basic configuration of your omniauth authentication against social networks.
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Added some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request