Skip to content

Instantly share code, notes, and snippets.

@Quebecisnice
Created April 3, 2012 00:57
Show Gist options
  • Save Quebecisnice/2288436 to your computer and use it in GitHub Desktop.
Save Quebecisnice/2288436 to your computer and use it in GitHub Desktop.
Rails OmniAuth snippet
def self.create_from_hash(registration_hash)
unless registration_hash || resigration_hash.empty?
return nil
end
provider = registration_hash[:provider]
info = registration_hash[:info]
user = User.new
if info.password
user.password = info.password
else
user.password = Devise.friendly_token[0,20]
end
user.password_confirmation = user.password
if provider == "seznam" || provider == "open_id"
user.email = info.nickname
else
user.email = info.email
end
# if data.first_name && info.last_name
# user.first_name = info.first_name if user.first_name.blank?
# user.last_name = info.last_name if user.last_name.blank?
# elsif data.name
# user.full_name = info.name if user.full_name.blank?
# user.first_name = info.name.split(' ')[0] if user.first_name.blank?
# user.last_name = info.name.split(' ')[1] if user.last_name.blank?
# end
# twitter: čtverec 48x48; odstranit "_normal" ze stringu pro plnou velikost obrázku
# facebook: čtverec 50x50; dál už se s tím nic nedá dělat
# user.image = info.image if user.image.blank?
# user.gender = registration_hash['extra']['gender'] if user.gender.blank?
if registration_hash[:skip_confirmation] == true
# user.confirm!
end
user
end
def process_callback
registration_hash = @omniauth
# Find if an authentication token for this provider and user id already exists
authentication = Authentication.find_by_provider_and_uid(@omniauth['provider'], @omniauth['uid'])
if authentication # We found an authentication
if user_signed_in? && (authentication.user.id != current_user.id)
flash[:error] = I18n.t "controllers.omniauth_callbacks.process_callback.error.account_already_taken",
:provider => @omniauth[:provider].capitalize,
:account => @omniauth[:uid] # registration_hash[:email]
redirect_to edit_user_registration_path(current_user) # edit_user_account_path
return
elsif
flash[:success] = "Signed in successfully."
sign_in_and_redirect(:user, authentication.user)
end
else # We could not find the authentication than create one
authentication = Authentication.new(:provider => @omniauth['provider'], :uid => @omniauth['uid'])
if user_signed_in?
authentication.user = current_user
else
registration_hash[:skip_confirmation] = true
authentication.user = User.find_by_email(registration_hash[:info][:email]) || User.create_from_hash(registration_hash)
end
end
@user = authentication.user
# save the authentication
authentication.token = @omniauth
authentication.provider = registration_hash[:provider]
authentication.uemail = registration_hash[:info][:email]
authentication.uname = registration_hash[:info][:name]
if !authentication.save
logger.error authentication.errors
logger.debug authentication.to_yaml
logger.debug @user.to_yaml
end
# If a user is signed in then he is trying to link a new account
if user_signed_in?
if authentication.save # This was a linking operation so send back the user to the account edit page
flash[:success] = I18n.t "controllers.omniauth_callbacks.process_callback.success.link_account",
:provider => @omniauth[:provider].capitalize,
:account => @omniauth[:uid] # registration_hash[:email]
else
flash[:error] = I18n.t "controllers.omniauth_callbacks.process_callback.error.link_account",
:provider => @omniauth[:provider].capitalize,
:account => @omniauth[:uid], # registration_hash[:email]
:errors => authentication.errors
end
redirect_to edit_user_registration_path(current_user) # edit_user_account_path
else
# This was a sign in operation so sign in the user and redirect it to his home page
if @user.save && authentication.save
flash[:success] = I18n.t "controllers.omniauth_callbacks.process_callback.success.sign_in",
:provider => @omniauth[:provider].capitalize,
:account => @omniauth[:uid] # registration_hash[:email]
sign_in_and_redirect(:user,@user)
else
session[:omniauth] = @omniauth.except('extra')
flash[:error] = I18n.t "controllers.omniauth_callbacks.process_callback.error.sign_in",
:provider => @omniauth[:provider],
:account => @omniauth[:uid] # registration_hash[:email]
redirect_to new_user_registration_url # new_registration_users_url
end
end
end
def process_callback
logger.debug "Processing #{@omniauth.provider}"
authentication = Authentication.find_by_provider_and_uid(@omniauth['provider'], @omniauth['uid'])
if authentication # Existing authentication
flash[:success] = I18n.t "devise.omniauth_callbacks.success", :kind => @omniauth['provider'].titleize
# flash[:notice] = "Signed in successfully."
sign_in_and_redirect(:user, authentication.user)
elsif current_user # User is linking new account
# při použití kombinace OmniAuth skrz Devise toto neumožní,
# ptž :omniauthable nemůže vytvořit uživatele, jeli uživatel přihlášen
current_user.authentications.create!(:provider => @omniauth['provider'], :uid => @omniauth['uid'],
:uemail => @omniauth['info']['email'], :uname => @omniauth['info']['name'], :token => @omniauth)
flash[:notice] = "New Account linked successfully."
redirect_to authentications_url
else # Registration of new user
user = User.new # create_from_hash(@omniauth)
user.apply_omniauth(@omniauth)
if user.save # && user.authentication.save
flash[:success] = I18n.t "devise.omniauth_callbacks.success", :kind => @omniauth['provider'].titleize
sign_in_and_redirect(:user, user)
else
session[:omniauth] = @omniauth.except('extra')
flash[:notice] = "Additional info required to proceed the registration."
flash[:notice] = I18n.t "devise.omniauth_callbacks.fail", :kind => @omniauth['provider'].titleize
redirect_to new_user_registration_url
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment