Skip to content

Instantly share code, notes, and snippets.

Created January 5, 2013 18:29
Show Gist options
  • Save anonymous/4462969 to your computer and use it in GitHub Desktop.
Save anonymous/4462969 to your computer and use it in GitHub Desktop.
def create_by_provider
# It's the Omniauth callback, Twitter and Facebook sends you here after authorization.
omniauth = request.env["omniauth.auth"]
if omniauth
registred_authorization = Authorization.find_by_provider_and_uid(omniauth['provider'], omniauth['uid'])
if not logged_in
if registred_authorization
user = User.find(registred_authorization.user_id)
session[:user_id] = user.id
redirect_to root_url, alert: "Successfully logged in."
else
@user = User.new
@user.external_provider = true
@user.name = omniauth["info"]["name"]
@user.email = omniauth["info"]["email"] if omniauth['provider'] != "twitter"
if @user.save
@user.authorizations.create!(:provider => omniauth['provider'], :uid => omniauth['uid'])
session[:user_id] = @user.id
redirect_to root_url, alert: "Successfully logged in."
else
redirect_to oops_url, alert: "We've found errors in our database with your external authentication, sorry."
end
end
else
if not registred_authorization
current_user.authorizations.create!(:provider => omniauth['provider'], :uid => omniauth['uid'])
redirect_to root_url, alert: "Successfully logged in."
else
redirect_to root_url
end
end
else
redirect_to oops_url, alert: "We've found strange errors dealing with your external authentication, sorry."
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment