2019-07-17 06:34:02 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class Users::AssociateAccountsController < ApplicationController
|
|
|
|
REDIS_PREFIX ||= "omniauth_reconnect"
|
|
|
|
|
|
|
|
def connect_info
|
|
|
|
auth = get_auth_hash
|
|
|
|
|
|
|
|
provider_name = auth.provider
|
|
|
|
authenticator = Discourse.enabled_authenticators.find { |a| a.name == provider_name }
|
|
|
|
raise Discourse::InvalidAccess.new(I18n.t('authenticator_not_found')) if authenticator.nil?
|
|
|
|
|
|
|
|
account_description = authenticator.description_for_auth_hash(auth)
|
|
|
|
|
|
|
|
render json: { token: params[:token], provider_name: provider_name, account_description: account_description }
|
|
|
|
end
|
|
|
|
|
|
|
|
def connect
|
|
|
|
auth = get_auth_hash
|
2019-12-03 03:05:53 -06:00
|
|
|
Discourse.redis.del "#{REDIS_PREFIX}_#{current_user&.id}_#{params[:token]}"
|
2019-07-17 06:34:02 -05:00
|
|
|
|
|
|
|
provider_name = auth.provider
|
|
|
|
authenticator = Discourse.enabled_authenticators.find { |a| a.name == provider_name }
|
|
|
|
raise Discourse::InvalidAccess.new(I18n.t('authenticator_not_found')) if authenticator.nil?
|
|
|
|
|
2021-07-01 07:44:58 -05:00
|
|
|
DiscourseEvent.trigger(:before_auth, authenticator, auth, session, cookies)
|
2019-07-17 06:34:02 -05:00
|
|
|
auth_result = authenticator.after_authenticate(auth, existing_account: current_user)
|
2021-07-01 07:44:58 -05:00
|
|
|
DiscourseEvent.trigger(:after_auth, authenticator, auth_result, session, cookies)
|
2019-07-17 06:34:02 -05:00
|
|
|
|
|
|
|
render json: success_json
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def get_auth_hash
|
|
|
|
token = params[:token]
|
2019-12-03 03:05:53 -06:00
|
|
|
json = Discourse.redis.get "#{REDIS_PREFIX}_#{current_user&.id}_#{token}"
|
2019-07-17 06:34:02 -05:00
|
|
|
raise Discourse::NotFound if json.nil?
|
|
|
|
|
|
|
|
OmniAuth::AuthHash.new(JSON.parse(json))
|
|
|
|
end
|
|
|
|
end
|