Repository navigation
OmniAuth: Overview
WARNING: THIS REQUIRES DEVISE FROM GIT REPOSITORY, MASTER BRANCH.
Devise 1.2 supports integration with OmniAuth. This wiki page will cover the basics to have this integration working using an OAuth provider as example.
The first step then is to add OmniAuth OAuth to our application, this can be done in our Gemfile:
gem "oa-oauth", :require => "omniauth/oauth"
Next, you need to declare the provider in your config/initializers/devise.rb:
config.omniauth :facebook, "APP_ID", "APP_SECRET"
And make your model omniauthable:
devise :omniauthable
Notice that Devise 1.2 only allows you to make one model omniauthable. If we have chosen a model named User and "devise_for :users" is already added to your config/routes.rb, Devise will create the following url methods:
- user_omniauth_authorize_path(provider)
- user_omniauth_callback_path(provider)
While you will never use the second one method directly, you only need to add the first one to your layouts in order to provide facebook authorization:
<%= link_to "Sign in with Facebook", user_omniauth_authorize_path(:facebook) %>
By clicking on this link, the user will be redirected to facebook. After inserting his credentials, we will be redirected back to your application. However this will fail as we have not implemented the callback yet. To implement a callback, the first step is to go back to our config/routes.rb file and tell Devise in which controller we will implement Omniauth callbacks:
devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" }
Now we just add the file "app/controllers/users/omniauth_callbacks_controller.rb":
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
end
The callback should be implemented as an action with the same name as the provider. Here is an example action for our facebook provider that we could add to our controller:
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
def facebook
# You need to implement the method below in your model
@user = Users.find_for_facebook_oauth(env["omniauth.auth"], current_user)
if @user.persisted?
flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Facebook"
sign_in_and_redirect @user, :event => :authentication
else
session["devise.facebook_data"] = env["omniauth.auth"]
redirect_to new_user_registration_url
end
end
end
This action has a few aspects worth describing:
-
All information retrieved from Facebook by OmniAuth is available as a hash at env["omniauth.auth"]. Check omniauth docs for more information.
-
In case a valid user is given from our model, we should sign it in. Notice we set a flash message using one of Devise's default messages, but that is up to you. Next, we sign the user in and redirect it. We pass the :event => :authentication to the sign_in_and_redirect method to force all authentication callbacks to be called.
-
In case the user is not persisted, we store the OmniAuth data in the session. Notice we store this data using "devise." as key namespace. This is useful because Devise removes all the data starting with "devise." from the session whenever an user signs in, so we get automatic session clean up. At the end, we redirect the user back to our registration form.
Notice that Devise RegistrationsController by default calls "User.new_with_session" before building a resource. This means that, if we need to copy data from session whenever an user is initialized before sign up, we just need to implement new_with_session in our model. Here is an example that copies the facebook email if available:
class User < ActiveRecord::Base
def self.new_with_session(params, session)
super.tap do |user|
if data = session["devise.facebook_data"]
user.email = data["email"]
end
end
end
end
Finally, if you want to allow your users to cancel sign up with Facebook, you can redirect them to "cancel_user_registration_path". This will remove all session data starting with "devise." and the new_with_session hook above will no longer be called.
OpenID and Google Apps examples
In order to use following features, you have to require openid store in Devise initializer. For example: require 'openid/store/filesystem'
- OpenID
Add following code to Devise initializer config.omniauth :open_id, OpenID::Store::Filesystem.new('/tmp')
and use this to your view link_to "sign in with google", user_omniauth_authorize_path(:open_id, :openid_url => "https://www.google.com/accounts/o8/id")
- Google Apps
Devise initializer: config.omniauth :google_apps, OpenID::Store::Filesystem.new('/tmp'), :domain => 'gmail.com' View: link_to 'GMail', user_omniauth_authorize_path(:google_apps)
And that is all you need! After you get your integration working, is time to write some tests:
http://github.com/plataformatec/devise/wiki/OmniAuth:-Testing