# Issues with custom OAuth provider & user\_id of access\_token being set to nil

**URL:** https://meta.discourse.org/t/issues-with-custom-oauth-provider-user-id-of-access-token-being-set-to-nil/103316
**Category:** Development
**Created:** [3 בדצמבר,‏ 2018,‏ 2:59am UTC](https://meta.discourse.org/t/issues-with-custom-oauth-provider-user-id-of-access-token-being-set-to-nil/103316 "2018-12-03T02:59:01Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Daniel\_Lopes](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/daniel_lopes/32/374589_2.png) [@Daniel\_Lopes](https://meta.discourse.org/u/Daniel_Lopes)
#### Post date: [3 בדצמבר,‏ 2018,‏ 2:59am UTC](https://meta.discourse.org/t/issues-with-custom-oauth-provider-user-id-of-access-token-being-set-to-nil/103316/1 "2018-12-03T02:59:02Z")

</div>

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.

```plaintext
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:

```plaintext
  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](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.

---

<div class="post-metadata">

### Author: ![Daniel\_Lopes](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/daniel_lopes/32/374589_2.png) [@Daniel\_Lopes](https://meta.discourse.org/u/Daniel_Lopes)
#### Post date: [3 בדצמבר,‏ 2018,‏ 5:06am UTC](https://meta.discourse.org/t/issues-with-custom-oauth-provider-user-id-of-access-token-being-set-to-nil/103316/3 "2018-12-03T05:06:54Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Daniel\_Lopes](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/daniel_lopes/32/374589_2.png) [@Daniel\_Lopes](https://meta.discourse.org/u/Daniel_Lopes)
#### Post date: [4 בדצמבר,‏ 2018,‏ 1:31am UTC](https://meta.discourse.org/t/issues-with-custom-oauth-provider-user-id-of-access-token-being-set-to-nil/103316/4 "2018-12-04T01:31:33Z")

</div>

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

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