2013-08-23 02:20:43 -04:00
|
|
|
class Auth::TwitterAuthenticator < Auth::Authenticator
|
|
|
|
|
|
|
|
def name
|
|
|
|
"twitter"
|
|
|
|
end
|
|
|
|
|
|
|
|
# TODO twitter provides all sorts of extra info, like website/bio etc.
|
|
|
|
# it may be worth considering pulling some of it in.
|
|
|
|
def after_authenticate(auth_token)
|
|
|
|
|
|
|
|
result = Auth::Result.new
|
|
|
|
|
|
|
|
data = auth_token[:info]
|
|
|
|
|
2016-09-06 12:18:13 -04:00
|
|
|
result.email = data['email']
|
2013-08-23 02:20:43 -04:00
|
|
|
result.username = screen_name = data["nickname"]
|
2016-09-06 12:18:13 -04:00
|
|
|
result.name = data["name"]
|
2013-08-23 02:20:43 -04:00
|
|
|
twitter_user_id = auth_token["uid"]
|
|
|
|
|
2016-09-06 12:18:13 -04:00
|
|
|
if result.email.present?
|
|
|
|
result.email_valid = true
|
|
|
|
end
|
|
|
|
|
2013-08-23 02:20:43 -04:00
|
|
|
result.extra_data = {
|
|
|
|
twitter_user_id: twitter_user_id,
|
|
|
|
twitter_screen_name: screen_name
|
|
|
|
}
|
|
|
|
|
2014-05-06 09:41:59 -04:00
|
|
|
user_info = TwitterUserInfo.find_by(twitter_user_id: twitter_user_id)
|
2013-08-23 02:20:43 -04:00
|
|
|
|
|
|
|
result.user = user_info.try(:user)
|
2016-09-06 12:18:13 -04:00
|
|
|
if !result.user && result.email.present? && result.user = User.find_by_email(result.email)
|
|
|
|
TwitterUserInfo.create(
|
|
|
|
user_id: result.user.id,
|
|
|
|
screen_name: screen_name,
|
|
|
|
twitter_user_id: twitter_user_id
|
|
|
|
)
|
|
|
|
end
|
2013-08-23 02:20:43 -04:00
|
|
|
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
def after_create_account(user, auth)
|
|
|
|
data = auth[:extra_data]
|
|
|
|
TwitterUserInfo.create(
|
|
|
|
user_id: user.id,
|
|
|
|
screen_name: data[:twitter_screen_name],
|
|
|
|
twitter_user_id: data[:twitter_user_id]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2013-08-25 21:04:16 -04:00
|
|
|
def register_middleware(omniauth)
|
|
|
|
omniauth.provider :twitter,
|
|
|
|
:setup => lambda { |env|
|
|
|
|
strategy = env["omniauth.strategy"]
|
|
|
|
strategy.options[:consumer_key] = SiteSetting.twitter_consumer_key
|
|
|
|
strategy.options[:consumer_secret] = SiteSetting.twitter_consumer_secret
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2013-08-23 02:20:43 -04:00
|
|
|
end
|