A user, Jim.Morrison, reports an issue with OAuth2 user linking in Discourse where users are repeatedly prompted to create new accounts instead of linking their existing ones. The problem occurs when users try to log in via OAuth after previously registering with a username and password. The user attempts to resolve this by linking Auth0 credentials to a Discourse user ID using the PluginStore.set command, but the change no longer persists. Falco clarifies that the method is outdated and that the plugin now uses the UserAssociatedAccount table. Falco provides the correct command to create a new UserAssociatedAccount, but Jim.Morrison encounters a PG::UniqueViolation error due to a duplicate key, indicating the account is already linked. The discussion highlights the need to check for existing associations before creating new ones.
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:
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.
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.
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
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