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

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: https://gist.github.com/danielvlopes/c265aeff5a7170512a8f3cb7c8d26d06

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

Thank you.

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.

5 Likes

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

https://gist.github.com/danielvlopes/25f6fc621697d71f3247a11eb47effe8

3 Likes