SHARE
TWEET

Untitled

a guest Mar 20th, 2016 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -----------------------User.rb-----------------------
  2. def self.find_for_twitter_oauth(auth, signed_in_resource=nil)
  3.   user = User.where(:provider => auth.provider, :uid => auth.uid).first
  4.   unless user
  5.     user = User.create(name:     auth.info.nickname,
  6.                      provider: auth.provider,
  7.                      uid:      auth.uid,
  8.                      email:    User.dummy_email(auth),
  9.                      password: Devise.friendly_token[0,20]
  10.                     )
  11.   end
  12.   user
  13. end
  14.  
  15. def self.create_unique_string
  16.   SecureRandom.uuid
  17. end
  18.  
  19.   # Create dummy email for OAuth
  20. def self.dummy_email(auth)
  21.   "#{auth.uid}-#{auth.provider}@example.com"
  22. end
  23.    
  24. class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
  25.  
  26.   def twitter
  27.   # You need to implement the method below in your model
  28.     @user = User.find_for_twitter_oauth(request.env["omniauth.auth"], current_user)
  29.  
  30.     if @user.persisted?
  31.       set_flash_message(:notice, :success, :kind => "Twitter") if is_navigational_format?
  32.       sign_in_and_redirect @user, :event => :authentication
  33.     else
  34.       session["devise.twitter_data"] = request.env["omniauth.auth"].except("extra")
  35.       redirect_to new_user_registration_url
  36.     end
  37.   end
  38. end
  39.    
  40. -------------------routes.rb---------------------------
  41.  devise_for :users, controllers: {
  42.                      sessions: "users/sessions",
  43.                      registrations: "registrations",
  44.                      omniauth_callbacks: "users/omniauth_callbacks"
  45.                    }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top