Created
January 29, 2014 18:05
-
-
Save JoshReedSchramm/8693477 to your computer and use it in GitHub Desktop.
Setting user data from social
This file contains hidden or 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
# In registrations controller add a private method | |
private | |
def default_data_from_social | |
defaults = {:user => {}, :identity => {}} | |
if session["devise.google_data"] | |
user_data = session["devise.google_data"] | |
@service = "Google" | |
elsif session["devise.facebook_data"] | |
user_data = session["devise.facebook_data"] | |
@service = "Facebook" | |
elsif session["devise.twitter_data"] | |
user_data = session["devise.twitter_data"] | |
@service = "Twitter" | |
end | |
if user_data.present? | |
defaults[:user][:first_name] = user_data.info["first_name"] | |
defaults[:user][:last_name] = user_data.info["last_name"] | |
defaults[:user][:nickname] = user_data.info["nickname"] | |
defaults[:user][:email] = user_data.info["email"] | |
defaults[:identity][:uid] = user_data.uid | |
defaults[:identity][:provider] = user_data.provider | |
defaults[:identity][:name] = user_data.info["name"] | |
end | |
defaults | |
end | |
# then in the new action | |
# NEW | |
@user = User.new(default_data_from_social) | |
# I also created a controller concern called SocialSignin | |
module Mobile | |
module SocialSignin | |
def self.included(receiver) | |
receiver.class_eval do | |
def setup_identities(user) | |
Identity.create_with_oauth(user, session["devise.google_data"]) if session["devise.google_data"] | |
Identity.create_with_oauth(user, session["devise.twitter_data"]) if session["devise.twitter_data"] | |
Identity.create_with_oauth(user, session["devise.facebook_data"]) if session["devise.facebook_data"] | |
end | |
end | |
end | |
end | |
end | |
# you include this in the memberships controller with | |
include Mobile::SocialSignin | |
# and then call it in create after you call save or update attributes with | |
setup_identities(@user) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment