Skip to content

Instantly share code, notes, and snippets.

@gumayunov
Created November 25, 2011 14:35
Show Gist options
  • Save gumayunov/1393657 to your computer and use it in GitHub Desktop.
Save gumayunov/1393657 to your computer and use it in GitHub Desktop.
AuthenticationService example
class AuthenticationsController < ApplicationController
def create
service = AuthenticationService
response = if current_user.present?
service.join(
:omniauth_hash => request.env['omniauth'],
:user => current_user
)
else
service.authenticate_or_register(
:omniauth_hash => request.env['omniauth']
)
end
case response.state
when :user_found
sign_in_and_redirect(:user, response[:user])
when :user_created
sign_in(:user, response[:user])
redirect_to welcome_from_social_network_path(response[:provider])
when :authentication_joined
url = if request.referer =~ /.+settings$/ #From settings page
settings_user_path(response[:user])
else
find_friends_from_path(response[:provider])
end
redirect_to url
when :user_data_incomplete
send_user_data_form(response)
else
redirect_to new_user_registration_url
end
end
def finish
user_data = session[:saved_response][:user_data].merge(params)
auth_hash = session[:saved_response][:auth_hahs]
session.delete :saved_response
response = AuthenticationService.register(
:user_data => user_data,
:auth_hash => auth_hash
)
case response.state
when :user_data_incomplete
send_user_data_form(response)
when :user_created
sign_in(:user, response[:user])
redirect_to welcome_from_social_network_path(response[:provider])
else
redirect_to new_user_registration_url
end
end
private
def send_user_data_form(response)
@user = response[:user]
session[:saved_response] = response.to_hash
render :need_more_information, :layout => "registration_layout"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment