FEATURE: Revoke and reconnect for Twitter logins

This commit is contained in:
David Taylor
2018-07-27 12:28:51 +01:00
parent c74dd2fa08
commit 9c72c00206
3 changed files with 76 additions and 4 deletions

View File

@ -13,7 +13,26 @@ class Auth::TwitterAuthenticator < Auth::Authenticator
info&.email || info&.screen_name || ""
end
def after_authenticate(auth_token)
def can_revoke?
true
end
def revoke(user, skip_remote: false)
info = TwitterUserInfo.find_by(user_id: user.id)
raise Discourse::NotFound if info.nil?
# We get a token from twitter upon login but do not need it, and do not store it.
# Therefore we do not have any way to revoke the token automatically on twitter's end
info.destroy!
true
end
def can_connect_existing_user?
true
end
def after_authenticate(auth_token, existing_account: nil)
result = Auth::Result.new
data = auth_token[:info]
@ -35,9 +54,21 @@ class Auth::TwitterAuthenticator < Auth::Authenticator
user_info = TwitterUserInfo.find_by(twitter_user_id: twitter_user_id)
result.user = user_info.try(:user)
if existing_account && (user_info.nil? || existing_account.id != user_info.user_id)
user_info.destroy! if user_info
result.user = existing_account
user_info = TwitterUserInfo.create!(
user_id: result.user.id,
screen_name: result.username,
twitter_user_id: twitter_user_id,
email: result.email
)
else
result.user = user_info&.user
end
if (!result.user) && result.email_valid && (result.user = User.find_by_email(result.email))
TwitterUserInfo.create(
TwitterUserInfo.create!(
user_id: result.user.id,
screen_name: result.username,
twitter_user_id: twitter_user_id,