# 0Auth2 user linking within Discourse

**URL:** https://meta.discourse.org/t/0auth2-user-linking-within-discourse/127618
**Category:** SSO
**Tags:** oauth2
**Created:** [September 4, 2019, 4:53pm UTC](https://meta.discourse.org/t/0auth2-user-linking-within-discourse/127618 "2019-09-04T16:53:25Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![Jim.Morrison](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jim.morrison/32/162161_2.png) [@Jim.Morrison](https://meta.discourse.org/u/Jim.Morrison)
#### Post date: [September 4, 2019, 4:53pm UTC](https://meta.discourse.org/t/0auth2-user-linking-within-discourse/127618/1 "2019-09-04T16:53:25Z")

</div>

Occasionally a user will experience an issue where each time they attempt to login they are asked to setup a new Discourse account. This commonly happens either on legacy accounts which are just now logging back into Discourse, or they signed up with username and password and are now trying to login as OAuth. In the past to resolve this, their Auth0 OAuth2 credentials need to be linked to their user ID in Discourse by running the below command from rails console within the discourse instance:

`PluginStore.set('oauth2_basic', 'oauth2_basic_user_google-apps|tony.danza@aol.com', {"user_id":7235})`

The problem seems to be this is no longer sticking, in fact it returns `=>True` where previously it would return `Ok`. I am at a pry prompt where in the past I didn’t think that was the case. When I exit Pry it also removes me from Rails Console.

Any idea what may be occurring here? Thanks!

Leveraging this plugin: [GitHub - discourse/discourse-oauth2-basic: A basic OAuth2 plugin for use with Discourse · GitHub](https://github.com/discourse/discourse-oauth2-basic)

---

<div class="post-metadata">

### Author: ![Falco](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/falco/32/179432_2.png) [@Falco](https://meta.discourse.org/u/Falco)
#### Post date: [September 4, 2019, 5:35pm UTC](https://meta.discourse.org/t/0auth2-user-linking-within-discourse/127618/2 "2019-09-04T17:35:47Z")

</div>

That command is outdated, now the plugin uses the user associations table.

---

<div class="post-metadata">

### Author: ![Jim.Morrison](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jim.morrison/32/162161_2.png) [@Jim.Morrison](https://meta.discourse.org/u/Jim.Morrison)
#### Post date: [September 4, 2019, 6:15pm UTC](https://meta.discourse.org/t/0auth2-user-linking-within-discourse/127618/3 "2019-09-04T18:15:15Z")

</div>

Can you share an example of the command that I will now run @Falco in replacement of it? Thank you for you help 🙇‍♂️

---

<div class="post-metadata">

### Author: ![Jim.Morrison](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jim.morrison/32/162161_2.png) [@Jim.Morrison](https://meta.discourse.org/u/Jim.Morrison)
#### Post date: [September 6, 2019, 1:40pm UTC](https://meta.discourse.org/t/0auth2-user-linking-within-discourse/127618/4 "2019-09-06T13:40:11Z")

</div>

Following up for the latest command @Falco, thanks!

---

<div class="post-metadata">

### Author: ![Jim.Morrison](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jim.morrison/32/162161_2.png) [@Jim.Morrison](https://meta.discourse.org/u/Jim.Morrison)
#### Post date: [September 11, 2019, 1:32pm UTC](https://meta.discourse.org/t/0auth2-user-linking-within-discourse/127618/5 "2019-09-11T13:32:00Z")

</div>

Any update or insight on the command @Falco?

---

<div class="post-metadata">

### Author: ![Falco](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/falco/32/179432_2.png) [@Falco](https://meta.discourse.org/u/Falco)
#### Post date: [September 11, 2019, 2:12pm UTC](https://meta.discourse.org/t/0auth2-user-linking-within-discourse/127618/6 "2019-09-11T14:12:13Z")

</div>

This file shound give you an idea of how to construct the command you need:

> <https://github.com/discourse/discourse-oauth2-basic/blob/main/db/migrate/20190724055909_move_to_managed_authenticator.rb>

---

<div class="post-metadata">

### Author: ![Jim.Morrison](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jim.morrison/32/162161_2.png) [@Jim.Morrison](https://meta.discourse.org/u/Jim.Morrison)
#### Post date: [September 18, 2019, 10:35pm UTC](https://meta.discourse.org/t/0auth2-user-linking-within-discourse/127618/7 "2019-09-18T22:35:31Z")

</div>

Sorry for the delay, I had to upgrade discourse before I tried again. I gave the change a shot but didn’t experience success. Below is the example of what I have since tried that return `true`.

```plaintext
[1] pry(main)> PluginStore.set('oauth2_basic', 'user_associated_accounts_google-apps|tony.danza@gmail.com', {"user_id":1234})
=> true

```

---

<div class="post-metadata">

### Author: ![Jim.Morrison](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jim.morrison/32/162161_2.png) [@Jim.Morrison](https://meta.discourse.org/u/Jim.Morrison)
#### Post date: [September 20, 2019, 9:25pm UTC](https://meta.discourse.org/t/0auth2-user-linking-within-discourse/127618/8 "2019-09-20T21:25:21Z")

</div>

Following up on this topic, thanks for the help!

---

<div class="post-metadata">

### Author: ![Falco](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/falco/32/179432_2.png) [@Falco](https://meta.discourse.org/u/Falco)
#### Post date: [September 30, 2019, 9:37pm UTC](https://meta.discourse.org/t/0auth2-user-linking-within-discourse/127618/14 "2019-09-30T21:37:59Z")

</div>

Damn you pinged me 7 times while I was on vacation…

Instead of using the `PluginStore.set` method, you need to create a new `UserAssociatedAccount`, like for example:

```ruby
UserAssociatedAccount.create!(provider_name: 'oauth2_basic', provider_uid: 'google-apps|tony.danza@aol.com', user_id: 7235)

```

---

<div class="post-metadata">

### Author: ![Jim.Morrison](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jim.morrison/32/162161_2.png) [@Jim.Morrison](https://meta.discourse.org/u/Jim.Morrison)
#### Post date: [October 1, 2019, 2:40am UTC](https://meta.discourse.org/t/0auth2-user-linking-within-discourse/127618/15 "2019-10-01T02:40:30Z")

</div>

Sorry about that @Falco, we too have had a hard time when having Community members go on vacation and not being able to keep everyone in the loop. I hope you had a great vacation and I apologize.

I will give your suggestion a go tomorrow and share the results. Thank you again for your help 🙏

---

<div class="post-metadata">

### Author: ![Jim.Morrison](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jim.morrison/32/162161_2.png) [@Jim.Morrison](https://meta.discourse.org/u/Jim.Morrison)
#### Post date: [February 26, 2020, 2:53am UTC](https://meta.discourse.org/t/0auth2-user-linking-within-discourse/127618/16 "2020-02-26T02:53:49Z")

</div>

Hey there @Falco, following up on this front. When I try to run the above suggested command I receive the below:

```
root@ip-172-31-20-121-app:/var/www/discourse# rails c
NOTE: Inheriting Faraday::Error::ClientError is deprecated; use Faraday::ClientError instead. It will be removed in or after version 1.0
Faraday::Error::ClientError.inherited called from /var/www/discourse/plugins/discourse-github/gems/2.6.5/gems/octokit-4.14.0/lib/octokit/middleware/follow_redirects.rb:14.
[1] pry(main)> UserAssociatedAccount.create!(provider_name: 'oauth2_basic', provider_uid: 'google-apps|nico@auth0.com', user_id: 1722)
ActiveRecord::RecordNotUnique: PG::UniqueViolation: ERROR: duplicate key value violates unique constraint "associated_accounts_provider_uid"
DETAIL: Key (provider_name, provider_uid)=(oauth2_basic, google-apps|nico@auth0.com) already exists.
from /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rack-mini-profiler-1.1.3/lib/patches/db/pg.rb:69:in `async_exec_params'
Caused by PG::UniqueViolation: ERROR: duplicate key value violates unique constraint "associated_accounts_provider_uid"
DETAIL: Key (provider_name, provider_uid)=(oauth2_basic, google-apps|nico@auth0.com) already exists.

from /var/www/discours

```
