Cookie Based Authentication in discourse via Plugin

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

  def make_auth_request()
    url = ''
    uri = URI.parse(url)
    http =, uri.port)
    http.use_ssl = true    
    http.ssl_version = :SSLv3

    request = uri
    request["Cookie"] = "#{TOKEN_COOKIE}=#{@request.cookies[TOKEN_COOKIE]}"
    request["X_API_KEY"] = "#{X_API_KEY}"

    response = http.request(request)

  def get_auth_username()
    @request.cookies[TOKEN_COOKIE] = TOKEN_COOKIE_VALUE
    raw_info = make_auth_request(false)    
    return raw_info["data"]["username"]

  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


You really should be using our official SSO support.


@codinghorror: We were earlier planning to use SSO only. But it doesn’t seem to work for our scenario. We have main website let us say Now, the discourse will be running as If a user is loginned in, and when he lands on the, he should be automatically logined.

Also, if the user signs out from any of the domains, he should be logged out from the other one automatically. This is not going to be handled by SSO properly.

Can this be achieved through SSO?

If session cookie is found (user logged-in in main site) then just redirect to instead of doing the magic yourself.



In our case, we set the session cookie even for guest users. So, it’s not possible to make this identification by that.

Also, regarding the signout. What if someone clears the cookies on the parent website, then the user will still be logined at Relying on logout on the parent website to expire the session cookies of discourse might not work for all scenarios.

Then create a dedicated wildcard cookie / API endpoint in parent website. It should tell whether the user is logged_in or not.

If you use wildcard cookie then you can check the existence in Discourse itself.


To handle logout, your main site can use the Discourse API to log out the user from Discourse when the session on the main site is terminated. This requires two API calls, one to resolve the external ID from your main site to a Discourse user id, and one to log out said user.