Skip to content

Instantly share code, notes, and snippets.

@joshsmith
Created October 28, 2012 21:33
Show Gist options
  • Select an option

  • Save joshsmith/3970009 to your computer and use it in GitHub Desktop.

Select an option

Save joshsmith/3970009 to your computer and use it in GitHub Desktop.
class ServicesController < ApplicationController
def index
@services = current_user.services.all
end
def create
# get the service parameter from the Rails router
params[:service] ? service_route = params[:service] : service_route = 'no service (invalid callback)'
omniauth = request.env['omniauth.auth']
if omniauth and params[:service]
if service_route == 'facebook'
email = omniauth['info']['email']
first_name = omniauth['info']['first_name']
last_name = omniauth['info']['last_name']
uid = omniauth['uid']
# image = omniauth['info']['image']
provider = omniauth['provider']
end
# if service_route == 'twitter'
# email = omniauth['info']['email']
# first_name = ""
# last_name = ""
# uid = omniauth['uid']
# provider = omniauth['provider']
# end
if uid != '' and provider != ''
# Check if the user is already signed in
unless signed_in?
# We're going to create a new Facebook user
# Check if the user already signed in using this provider
auth = Service.find_by_provider_and_uid(provider, uid)
if auth
flash[:success] = 'Signed in successfully via ' + provider.capitalize + '.'
sign_in_and_redirect(:user, auth.user)
else
if email != ''
# Check if the user is already registered with this email
existing_user = User.find_by_email(email)
if existing_user
existing_user.services.create(:provider => provider,
:uid => uid, :first_name => first_name,
:last_name => last_name, :email => email)
flash[:success] = 'Sign in via ' + provider.capitalize + ' has been added to your account. Signed in successfully.'
sign_in_and_redirect(:user, existing_user)
else
# let's create a new user: register this user and add this authentication method for this user
username = SecureRandom.hex(16)
# new user, set email, a random password and take the name from the authentication service
user = User.new :email => email, :password => SecureRandom.hex(10), :first_name => first_name,
:last_name => last_name, :username => username
# if provider == 'twitter'
# user.sign_up_via_twitter = true
# end
# add this authentication service to our new user
user.services.build(:provider => provider, :uid => uid, :first_name => first_name,
:last_name => last_name, :email => email)
user.save!
flash[:success] = 'Your account on Artburst has been created via ' + provider.capitalize + '.'
sign_in_and_redirect(:user, user)
end
else
# No valid email has been provided
flash[:error] = service_route.capitalize + ' can not be used to sign up for Artburst since no valid email address has been provided. Please use another authentication provider or sign up by email. If you already have an account, please sign-in and add ' + service_route.capitalize + ' from your profile.'
redirect_to new_user_session_path
end
end
else
# Add service to an existing account
auth = Service.find_by_provider_and_uid(provider, uid)
if !auth
current_user.services.create(:provider => provider, :uid => uid,
:first_name => first_name, :last_name => last_name, :email => email)
flash[:notice] = 'Sign in via ' + provider.capitalize + ' has been added to your account.'
redirect_to edit_user_path(current_user.username)
else
flash[:notice] = service_route.capitalize + ' is already linked to your account.'
redirect_to edit_user_path(current_user.username)
end
end
else
flash[:error] = service_route.capitalize + ' returned invalid data for the user id.'
redirect_to new_user_session_path
end
else
# There was an error authenticating
flash[:error] = 'Error while authenticating via ' + service_route.capitalize + '.'
redirect_to new_user_session_path
end
end
def destroy
# remove an authentication service linked to the current user
@service = current_user.services.find(params[:id])
@service.destroy
redirect_to services_path
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment