2013-08-23 02:20:43 -04:00
|
|
|
class Auth::FacebookAuthenticator < Auth::Authenticator
|
|
|
|
|
|
|
|
def name
|
|
|
|
"facebook"
|
|
|
|
end
|
|
|
|
|
|
|
|
def after_authenticate(auth_token)
|
|
|
|
|
|
|
|
result = Auth::Result.new
|
|
|
|
|
|
|
|
session_info = parse_auth_token(auth_token)
|
|
|
|
facebook_hash = session_info[:facebook]
|
|
|
|
|
|
|
|
result.email = email = session_info[:email]
|
2013-12-17 12:29:29 -05:00
|
|
|
result.email_valid = true
|
2014-02-16 22:45:17 -05:00
|
|
|
result.name = facebook_hash[:name]
|
2013-08-23 02:20:43 -04:00
|
|
|
|
2013-08-26 03:36:20 -04:00
|
|
|
result.extra_data = facebook_hash
|
2013-08-23 02:20:43 -04:00
|
|
|
|
2014-05-06 09:41:59 -04:00
|
|
|
user_info = FacebookUserInfo.find_by(facebook_user_id: facebook_hash[:facebook_user_id])
|
2013-08-26 04:01:01 -04:00
|
|
|
result.user = user_info.try(:user)
|
2013-08-23 02:20:43 -04:00
|
|
|
|
2014-07-14 10:16:24 -04:00
|
|
|
if !result.user && !email.blank? && result.user = User.find_by_email(email)
|
2013-08-26 03:36:20 -04:00
|
|
|
FacebookUserInfo.create({user_id: result.user.id}.merge(facebook_hash))
|
2013-08-23 02:20:43 -04:00
|
|
|
end
|
|
|
|
|
2014-03-19 13:31:17 -04:00
|
|
|
if email.blank?
|
|
|
|
UserHistory.create(
|
|
|
|
action: UserHistory.actions[:facebook_no_email],
|
|
|
|
details: "name: #{facebook_hash[:name]}, facebook_user_id: #{facebook_hash[:facebook_user_id]}"
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2013-08-23 02:20:43 -04:00
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
def after_create_account(user, auth)
|
|
|
|
data = auth[:extra_data]
|
|
|
|
FacebookUserInfo.create({user_id: user.id}.merge(data))
|
|
|
|
end
|
|
|
|
|
2013-08-25 21:04:16 -04:00
|
|
|
def register_middleware(omniauth)
|
|
|
|
omniauth.provider :facebook,
|
|
|
|
:setup => lambda { |env|
|
|
|
|
strategy = env["omniauth.strategy"]
|
|
|
|
strategy.options[:client_id] = SiteSetting.facebook_app_id
|
|
|
|
strategy.options[:client_secret] = SiteSetting.facebook_app_secret
|
|
|
|
},
|
|
|
|
:scope => "email"
|
|
|
|
end
|
|
|
|
|
2013-08-23 02:20:43 -04:00
|
|
|
protected
|
|
|
|
|
|
|
|
def parse_auth_token(auth_token)
|
|
|
|
|
|
|
|
raw_info = auth_token["extra"]["raw_info"]
|
|
|
|
email = auth_token["info"][:email]
|
|
|
|
|
|
|
|
{
|
|
|
|
facebook: {
|
|
|
|
facebook_user_id: auth_token["uid"],
|
|
|
|
link: raw_info["link"],
|
|
|
|
username: raw_info["username"],
|
|
|
|
first_name: raw_info["first_name"],
|
|
|
|
last_name: raw_info["last_name"],
|
|
|
|
email: email,
|
|
|
|
gender: raw_info["gender"],
|
|
|
|
name: raw_info["name"]
|
|
|
|
},
|
|
|
|
email: email,
|
|
|
|
email_valid: true
|
|
|
|
}
|
|
|
|
|
|
|
|
end
|
2013-08-25 21:04:16 -04:00
|
|
|
|
|
|
|
|
2013-08-23 02:20:43 -04:00
|
|
|
end
|