Created
October 13, 2012 17:36
-
-
Save jwo/3885486 to your computer and use it in GitHub Desktop.
OmniAuth with Twitter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
config.omniauth :facebook, FACEBOOK_APP_ID, FACEBOOK_APP_SECRET, { :scope => 'user_status,publish_stream' } | |
config.omniauth :twitter, TWITTER_KEY, TWITTER_SECRET |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
= link_to "Sign in with Twitter", omniauth_authorize_path(:user, :twitter) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Users | |
class OmniauthCallbacksController < Devise::OmniauthCallbacksController | |
def facebook | |
oauthorize | |
end | |
def twitter | |
oauthorize | |
end | |
private | |
def oauthorize | |
@user = User.find_from_oauth_attrs(oauth_attrs) | |
if @user.persisted? | |
flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Facebook" | |
sign_in_and_redirect @user, :event => :authentication | |
else | |
if current_user | |
authorization = current_user.authorizations.build(:uid => oauth_attrs[:uid], | |
:provider => oauth_attrs[:provider], | |
:token => oauth_attrs[:token], | |
:secret => oauth_attrs[:secret]) | |
if authorization.save | |
flash[:notice] = "#{authorization.provider.capitalize} authorization has been added" | |
end | |
redirect_to edit_user_registration_path(current_user) | |
else | |
session["devise.oauth_attrs"] = oauth_attrs | |
redirect_to new_user_registration_url | |
end | |
end | |
end | |
def oauth_attrs | |
data = request.env['omniauth.auth'] | |
{ :provider => data.provider, | |
:uid => data.uid, | |
:email => data.info.email, | |
:first_name => data.info.first_name, | |
:last_name => data.info.last_name, | |
:token => data.credentials.token, | |
:secret => data.credentials.secret } | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks", | |
:registrations => "registrations" } | |
devise_scope :user do | |
delete '/users/:id/authorization/:auth_id' => "registrations#destroy_authorization", :as => :destroy_authorization | |
resource :profile, only: [:show] | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment