Skip to content

Instantly share code, notes, and snippets.

@salayhin
Created May 20, 2015 11:40
Show Gist options
  • Save salayhin/6498c678c8727098bd6a to your computer and use it in GitHub Desktop.
Save salayhin/6498c678c8727098bd6a to your computer and use it in GitHub Desktop.
# encoding: utf-8
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
before_filter :authenticate_user!, only: :stripe_connect
def facebook
auth = request.env['omniauth.auth']
@user = User.where(auth.slice('provider', 'uid')).first
if @user.present?
sign_in(@user, event: :authentication)
redirect_to session.has_key?(:return_url) ? session.delete(:return_url) : root_path
elsif auth['info']['email'].present? and email_available?(auth['info']['email'])
@user = User.create_from_omniauth_facebook auth
#sign_in_and_redirect @user, event: :authentication # this will throw if @user is not activated
set_flash_message(:notice, :success, kind: 'Facebook') if is_navigational_format?
sign_in(@user, event: :authentication)
redirect_to session.has_key?(:return_url) ? session.delete(:return_url) : root_path
else
sign_up_data = {
provider: auth['provider'],
uid: auth['uid'],
image: auth['info']['image'],
first_name: auth['info']['first_name'],
last_name: auth['info']['last_name'],
gender: auth['extra']['raw_info']['gender']
}
session[:sign_up_data] = sign_up_data
redirect_to request.referer.present? ? request.referer : root_path
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment