Issues with custom OAuth provider & user_id of access_token being set to nil


(Daniel Lopes) #1

Hey everyone,

My company is starting to offer access to our discourse as part of our main product, and we decided to build a custom OAuth integration so people can login with the existing account.

We created as an authenticator based on FB and Twitter ones from Discourse codebase, released that as an internal Discourse plugin, and turned our main app into an OAuth provider. So far so good and everything worked just fine in development, but unfortunately I’m getting a PG constraint error in production.

Failed to handle exception in exception app middleware : PG::NotNullViolation: ERROR:  null value in column "user_id" violates not-null constraint DETAIL:  Failing row contains (XYZ, null, zk320lKJVI... : INSERT INTO "user_auth_tokens" ("auth_token", "prev_auth_token", "user_agent", "client_ip", "rotated_at", "created_at", "updated_at") VALUES (...

I’m assuming my plugin is passing something wrong to OmniauthCallbacksController. More precisely here:

  def complete_response_data
    if @auth_result.user
      user_found(@auth_result.user)
    elsif SiteSetting.invite_only?
      @auth_result.requires_invite = true
    else
      session[:authentication] = @auth_result.session_data
    end
  end

This is the code of my authenticator: honcho_authenticator.rb · GitHub

If someone has experience with the authenticator part of Discourse that could shed some light, that would be very helpful.

Thank you.


(Daniel Lopes) #3

I found the error. I was expecting PluginStore to return a hash indexed by symbols but it’s actually index by strings. Doh!

Well, at least the code of my authenticator plugin can be useful to others.


(Daniel Lopes) #4

Here’s my updated authenticator plugin in case someone else needs something similar: