Skip to content

Instantly share code, notes, and snippets.

@conradchu
Created December 6, 2012 08:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save conradchu/4222694 to your computer and use it in GitHub Desktop.
Save conradchu/4222694 to your computer and use it in GitHub Desktop.
class Users::Devise::OmniauthCallbacksController < Devise::OmniauthCallbacksController
# guest trying to login with facebook
def facebook
self.process_login
end
# guest trying to login with twitter
def twitter
self.process_login
end
# failure action
def failure
flash[:alert] = "Authorization failed"
strategy = session[:sso][:strategy] if session[:sso] && session[:sso][:strategy]
order_type = session[:sso][:order_type] if session[:sso] && session[:sso][:order_type]
case strategy
when "add_provider"
@redirect_url = nil
when "ordering"
@redirect_url = step2_checkout_index_path(:login => true, :order_type => order_type)
else
@redirect_url = new_user_session_path
end
end
protected
def process_login
strategy = session[:sso][:strategy] if session[:sso] && session[:sso][:strategy]
case strategy
when "signup"
self.sso_signup_manual
when "add_provider"
self.sso_login_add_provider
when "ordering"
self.sso_login_ordering
else
self.sso_login_manual
end
end
def sso_signup_manual
omniauth = request.env["omniauth.auth"]
if user_signed_in?
flash[:notice] = "You're already signed in!"
@redirect_url = users_cp_path
else
authentication = Authentication.find_by_provider_and_uid(omniauth["provider"], omniauth["uid"])
if authentication
# Update access token
authentication.update_attribute('access_token', omniauth["credentials"]["token"])
# sign user in
sign_in(:user, authentication.user)
flash[:mixpanel_analytics] = "#{authentication.provider.titleize} login success"
flash[:km_analytics] = "['record', '#{authentication.provider.titleize} login success']"
flash[:notice] = "Welcome back, #{authentication.user.first_name}!"
@redirect_url = welcome_users_cp_path
else
if omniauth["user_info"]["email"]
# find existing account with this email address?
user = User.find_by_email(omniauth["user_info"]["email"])
if user
h = { :provider => omniauth["provider"], :uid => omniauth["uid"], :access_token => omniauth["credentials"]["token"] }
authentication = user.authentications.create(h)
# sign user in
sign_in(:user, user)
flash[:mixpanel_analytics] = "#{authentication.provider.titleize} login success"
flash[:km_analytics] = "['record', '#{authentication.provider.titleize} login success']"
flash[:notice] = "Welcome back, #{user.first_name}!"
@redirect_url = welcome_users_cp_path
end
end
# found no existing authentication, prompt user to create a new account by redirecting to registration
# Devise removes all the data starting with "devise." from the session whenever a user signs in
session["devise.omniauth"] = omniauth
@redirect_url = new_user_registration_path(:sso => true)
end
end
end
def sso_login_add_provider
omniauth = request.env["omniauth.auth"]
if user_signed_in?
authentication = Authentication.find_by_provider_and_uid(omniauth["provider"], omniauth["uid"])
if authentication
if authentication.user == current_user # same user as current user, everything is good
authentication.access_token = omniauth["credentials"]["token"]
authentication.save
else
flash[:alert] = "It looks like this #{authentication.provider.titleize} account is linked to another Munchery account #{authentication.user.masked_email}"
end
else # found no existing authentication, so create one
authentication = current_user.authentications.create(:provider => omniauth["provider"], :uid => omniauth["uid"], :access_token => omniauth["credentials"]["token"])
flash[:mixpanel_analytics] = "Connected account with #{authentication.provider.titleize}"
flash[:km_analytics] = "['record', 'Connected account with #{authentication.provider.titleize}']"
flash[:notice] = "Authentication successful."
end
else
flash[:alert] = "You need to sign in first"
@redirect_url = new_user_session_path
end
end
def sso_login_ordering
omniauth = request.env["omniauth.auth"]
order_type = session[:sso][:order_type] if session[:sso] && session[:sso][:order_type]
if user_signed_in?
flash[:notice] = "You're already signed in"
@redirect_url = step2_checkout_index_path(:order_type => order_type)
else
authentication = Authentication.find_by_provider_and_uid(omniauth["provider"], omniauth["uid"])
if authentication
# Update access token
authentication.update_attribute('access_token', omniauth["credentials"]["token"])
# sign user in
sign_in(:user, authentication.user)
flash[:mixpanel_analytics] = "#{authentication.provider.titleize} login success"
flash[:km_analytics] = "['record', '#{authentication.provider.titleize} login success']"
flash[:notice] = "Welcome back, #{authentication.user.first_name}!"
@redirect_url = step2_checkout_index_path(:order_type => order_type)
else
if omniauth["user_info"]["email"]
# find existing account with this email address?
user = User.find_by_email(omniauth["user_info"]["email"])
if user
h = { :provider => omniauth["provider"], :uid => omniauth["uid"], :access_token => omniauth["credentials"]["token"] }
authentication = user.authentications.create(h)
# sign user in
sign_in(:user, user)
flash[:mixpanel_analytics] = "#{authentication.provider.titleize} login success"
flash[:km_analytics] = "['record', '#{authentication.provider.titleize} login success']"
flash[:notice] = "Welcome back, #{user.first_name}!"
@redirect_url = step2_checkout_index_path(:order_type => order_type)
end
end
# found no existing authentication, prompt user to create a new account by redirecting to registration
# Devise removes all the data starting with "devise." from the session whenever a user signs in
session["devise.omniauth"] = omniauth
flash[:alert] = "You aren't registered. Please proceed by entering your information"
@redirect_url = step2_checkout_index_path(:order_type => order_type)
end
end
end
def sso_login_manual
omniauth = request.env["omniauth.auth"]
if user_signed_in?
flash[:notice] = "You're already signed in"
@redirect_url = users_cp_path
else
authentication = Authentication.find_by_provider_and_uid(omniauth["provider"], omniauth["uid"])
if authentication
# Update access token
authentication.update_attribute('access_token', omniauth["credentials"]["token"])
# sign user in
sign_in(:user, authentication.user)
flash[:mixpanel_analytics] = "#{authentication.provider.titleize} login success"
flash[:km_analytics] = "['record', '#{authentication.provider.titleize} login success']"
flash[:notice] = "Welcome back, #{authentication.user.first_name}!"
@redirect_url = session[:user_return_to] || menus_path
else
if omniauth["user_info"]["email"]
# find existing account with this email address?
user = User.find_by_email(omniauth["user_info"]["email"])
if user
h = { :provider => omniauth["provider"], :uid => omniauth["uid"], :access_token => omniauth["credentials"]["token"] }
authentication = user.authentications.create(h)
# sign user in
sign_in(:user, user)
flash[:mixpanel_analytics] = "#{authentication.provider.titleize} login success"
flash[:km_analytics] = "['record', '#{authentication.provider.titleize} login success']"
flash[:notice] = "Welcome back, #{user.first_name}!"
@redirect_url = session[:user_return_to] || menus_path
end
end
# found no existing authentication, prompt user to create a new account by redirecting to registration
# Devise removes all the data starting with "devise." from the session whenever a user signs in
session["devise.omniauth"] = omniauth
@redirect_url = new_user_registration_path(:sso => true)
end
end
end
def redirect_url(default_redirect=nil)
default_redirect ||= menus_path
if session[:sso] && session[:sso][:return]
return session[:sso][:return]
else
return default_redirect
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment