Fixed SSO login with company

pull/624/head
Kashiftariq1997 3 months ago
parent 9640e409f1
commit 6e8f556914

@ -65,24 +65,20 @@ class SsoLoginController < ApplicationController
end end
def find_or_create_user(email, first_name, last_name, company_id = nil, company_name = nil) def find_or_create_user(email, first_name, last_name, company_id = nil, company_name = nil)
# Find or create account based on company_id
account = find_or_create_account_by_company(company_id, company_name)
# Try to find existing user by email (email is unique globally) # Try to find existing user by email (email is unique globally)
user = User.find_by(email: email) user = User.find_by(email: email)
if user if user
# User exists - check if they're in the correct account # User exists - KEEP them in their existing account
if user.account_id != account.id # Don't move users between accounts to preserve their data (files, templates, etc.)
# User exists but in a different account account = user.account
# Move user to the correct account if company_id is provided Rails.logger.info("User #{email} exists in account #{account.id} (#{account.name}). Keeping in existing account.")
if company_id.present?
Rails.logger.info("Moving user #{email} from account #{user.account_id} to account #{account.id} (company_id: #{company_id})") # Log if company_id points to a different account (for debugging)
user.update(account_id: account.id) if company_id.present? || company_name.present?
else expected_account = find_or_create_account_by_company(company_id, company_name)
# If no company_id provided, keep user in existing account but log warning if expected_account && user.account_id != expected_account.id
Rails.logger.warn("User #{email} exists in account #{user.account_id} but company_id not provided in token") Rails.logger.warn("User #{email} is in account #{user.account_id} but company_id #{company_id} points to account #{expected_account.id}. User kept in existing account to preserve data.")
account = user.account # Use existing account
end end
end end
@ -96,7 +92,9 @@ class SsoLoginController < ApplicationController
return user return user
end end
# User doesn't exist, create a new one in the specified account # User doesn't exist - create account based on company_id and create new user
account = find_or_create_account_by_company(company_id, company_name)
# Generate a random password for the new user # Generate a random password for the new user
password = SecureRandom.hex(16) password = SecureRandom.hex(16)

Loading…
Cancel
Save