Fix-OAuth-Token-Commands
Rails Console Commands to Fix OAuth Token Issue For fixing the OAuth token issue for user ID 253197, run the following commands in the Rails console:
Fix-OAuth-Token-Commands
---
Rails Console Commands to Fix OAuth Token Issue
For fixing the OAuth token issue for user ID 253197, run the following commands in the Rails console.
---
1. Find the user by ID
user = User.find(253197)
---
2. Get the user's registration site ID
site_id = user.registration_site_id
site = Site.find(site_id)
puts "User {user.email} belongs to site: {site.name} (ID: {site_id})"
---
3. Find the OAuth application for this site
app = site.oauth_applications.first
if app.nil?
puts "No OAuth application found for site {site_id}. Creating one..."
app = Doorkeeper::Application.create(
owner: site,
name: site.name,
redirect_uri: 'urn:ietf:wg:oauth:2.0:oob'
)
end
---
4. Check if an OAuth token already exists
existing_token = Doorkeeper::AccessToken.where(
resource_owner_id: user.to_global_id.to_s,
application_id: app.id
).first
if existing_token && !existing_token.revoked_at
puts "Valid token already exists: {existing_token.token}"
else
# Step 5: Create a new OAuth token for this user
token = Doorkeeper::AccessToken.create!(
application: app,
resource_owner_id: user.to_global_id.to_s,
use_refresh_token: true,
scopes: 'read write'
)
puts "Created new token: {token.token}"
puts "Token created successfully for user {user.email} (ID: {user.id})"
end
---
5. Verify the token works
begin
test_token = Doorkeeper::AccessToken.find_or_create_for(
app,
user.to_global_id.to_s,
Doorkeeper::OAuth::Scopes.from_string('read write'),
nil,
true
)
puts "Verification successful. Token: {test_token.token}"
rescue => e
puts "Error during verification: {e.message}"
end
---
6. Alternative pattern (simpler):
This will create the token if it doesn't exist, or return the existing one.
token = Doorkeeper::AccessToken.find_or_create_for(
app,
user.to_global_id.to_s,
Doorkeeper::OAuth::Scopes.from_string('read write'),
nil,
true
)
puts "Token: {token.token}"
---
Checking Cyclum Integration (if needed)
If there might also be a Cyclum integration issue (as seen previously with user ID mismatches), you can check the Cyclum database.
---
Connect to Cyclum DB from a separate console
Note: You'll need to have database credentials configured.
---
Check for users in Cyclum with this RealSavvy ID – using ActiveRecord
cyclum_users = User.where(realsavvy_id: '253197').select(:id, :auto_assign_token, :email).to_a
if cyclum_users.empty?
puts "No users found in Cyclum with RealSavvy ID 253197"
elsif cyclum_users.size > 1
puts "WARNING: Multiple users found in Cyclum with the same RealSavvy ID 253197:"
cyclum_users.each do |user|
puts " User ID: {user.id}, Email: {user.email}"
end
puts "This indicates a data integrity issue that should be investigated."
puts "Choose which user to proceed with or fix all:"
else
puts "Found 1 user in Cyclum with RealSavvy ID 253197"
end
---
Process each user found
cyclum_users.each do |cyclum_user|
def decode_base64_token(token)
require 'base64'
Base64.decode64(token)
end
def extract_user_id_from_token(token_data)
token_data.to_s.split('User/').last.to_s.gsub('"', '').gsub('}', '').strip
end
if cyclum_user.auto_assign_token.present?
token_data = decode_base64_token(cyclum_user.auto_assign_token)
referenced_user_id = extract_user_id_from_token(token_data)
if referenced_user_id != '253197'
puts "TOKEN MISMATCH for Cyclum user {cyclum_user.id}: Token references user {referenced_user_id} but should be 253197"
site_id_match = token_data.to_s.match(/Site\/(\d+)/)
site_id = site_id_match ? site_id_match[1] : "{site_id}"
correct_token = Base64.encode64(%Q).strip
puts "Run this SQL to fix:"
puts "UPDATE users SET auto_assign_token = '{correct_token}' WHERE id = '{cyclum_user.id}';"
else
puts "Token for Cyclum user {cyclum_user.id} is correctly referencing RealSavvy user 253197"
end
else
puts "Cyclum user {cyclum_user.id} has no auto_assign_token set"
end
end
---
Batch fix example for all mismatched users (if multiple were found):
if cyclum_users.size > 1
puts "--- Example of batch fix for all mismatched users: ---"
puts "UPDATE users SET auto_assign_token = 'eyJhdWQiOiJnaWQ6Ly9yZWFsLXNhdnZ5L1NpdGUvMTM3OSIsInN1YiI6ImdpZDovL3JlYWwtc2F2dnkvVXNlci8yNTMxOTcifQ==' WHERE realsavvy_id = '253197';"
puts "--- (Update the Site ID in the token if needed) ---"
end
---
Helper methods (optional, for reference):
def decode_base64_token(token)
require 'base64'
Base64.decode64(token)
end
def extract_user_id_from_token(token_data)
token_data.to_s.split('User/').last.to_s.gsub('"', '').gsub('}', '').strip
end
---
Remember to replace any placeholder values with actual values from your environment.