Hi All,
I am trying to use cookie-based authentication in discourse (session is being controlled by parent domain). Here, I am making an API call to the parent website backend to find the user from the session cookie. The issue here is that when a user gets changed, then on the client side, a user has to refresh the page 5-10 requests in order to the user being shown logined (i.e. the user avatar is shown and other user login related features like option of logout are shown). I am not able to understand the reason behind the delay. In the logs, the current user returns the desired user from the first call itself, but it’s not propagated for some time. I am not able to understand reason behind it. Any tips in this regard please? The code for the plugin is as follows.
class ExCurrentUserProvider < Auth::DefaultCurrentUserProvider
TOKEN_COOKIX ||= "logged_in".freeze
LOCAL_SERVER ||= true
TOKEN_COOKIE ||= "SAMPLE_TOKEN_COOKIE"
X_API_KEY = "SAMPLE_X_API_KEY"
def make_auth_request()
url = 'https://www.example.com/api/auth'
uri = URI.parse(url)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.ssl_version = :SSLv3
request = Net::HTTP::Get.new uri
request["Cookie"] = "#{TOKEN_COOKIE}=#{@request.cookies[TOKEN_COOKIE]}"
request["X_API_KEY"] = "#{X_API_KEY}"
response = http.request(request)
response.body
end
def get_auth_username()
@request.cookies[TOKEN_COOKIE] = TOKEN_COOKIE_VALUE
raw_info = make_auth_request(false)
return raw_info["data"]["username"]
end
def current_user
return @env[CURRENT_USER_KEY] if @env.key?(CURRENT_USER_KEY)
username = get_auth_username
user = User.find_by(username: username)
if !user.nil?
@env[CURRENT_USER_KEY] = user
end
user
end
end