Skip to content

Better handling of existing users #5

@batonac

Description

@batonac

At the moment the system can incorrectly attempt to create a user record which already exists:

Traceback with variables (most recent call last):
  File "apps/frappe/frappe/app.py", line 104, in application
    validate_auth()
      request = <Request 'http://gapchurchleague.com/website_script.js' [GET]>
      response = None
      e = ('User', 'john@example.com', IntegrityError(1062, "Duplicate entry 'john@example.com' for key 'PRIMARY'"))
  File "apps/frappe/frappe/auth.py", line 637, in validate_auth
    validate_auth_via_hooks()
      authorization_header = ['']
  File "apps/frappe/frappe/auth.py", line 738, in validate_auth_via_hooks
    frappe.get_attr(auth_hook)()
      auth_hook = 'jwt_auth.auth.validate_auth'
  File "apps/jwt_auth/jwt_auth/auth.py", line 290, in validate_auth
    SessionJWTAuth().validate_auth()
  File "apps/jwt_auth/jwt_auth/auth.py", line 87, in validate_auth
    self.auth()
      self = jwt_auth.auth.JWTAuth(claims={'aud': ['...'], 'email': 'john@example.com', 'exp': 1769347834, 'iat': 1769261434, 'nbf': 1769261434, 'iss': 'https://avunu.cloudflareaccess.com', 'type': 'app', 'identity_nonce': 'wmVzs4Tq6dmJjlZx', 'sub': '0fbfb578-a1b6-54ce-b118-630a66622e0a', 'country': 'US'}, http_status_code=None, path=None, redirect_to=None, settings=JWT Auth Settings (JWT Auth Settings), token='............-...-...-...', user_email='john@example.com')
  File "apps/jwt_auth/jwt_auth/auth.py", line 67, in auth
    self.register_user(user_email)
      self = jwt_auth.auth.JWTAuth(claims={'aud': ['...'], 'email': 'john@example.com', 'exp': 1769347834, 'iat': 1769261434, 'nbf': 1769261434, 'iss': 'https://avunu.cloudflareaccess.com', 'type': 'app', 'identity_nonce': 'wmVzs4Tq6dmJjlZx', 'sub': '0fbfb578-a1b6-54ce-b118-630a66622e0a', 'country': 'US'}, http_status_code=None, path=None, redirect_to=None, settings=JWT Auth Settings (JWT Auth Settings), token='............-...-...-...', user_email='john@example.com')
      user_email = 'john@example.com'
      Contact = "tabContact"
      ContactEmail = "tabContact Email"
      user_exists = [frappe.types.frappedict._dict{'user': None}]
  File "apps/jwt_auth/jwt_auth/auth.py", line 197, in register_user
    user.insert(ignore_permissions=True)
      self = jwt_auth.auth.JWTAuth(claims={'aud': ['...'], 'email': 'john@example.com', 'exp': 1769347834, 'iat': 1769261434, 'nbf': 1769261434, 'iss': 'https://avunu.cloudflareaccess.com', 'type': 'app', 'identity_nonce': 'wmVzs4Tq6dmJjlZx', 'sub': '0fbfb578-a1b6-54ce-b118-630a66622e0a', 'country': 'US'}, http_status_code=None, path=None, redirect_to=None, settings=JWT Auth Settings (JWT Auth Settings), token='............-...-...-...', user_email='john@example.com')
      user_email = 'john@example.com'
      contact = 'John Doe'
      contact_doc = Contact (John Doe)
      first_name = 'John'
      user = User (john@example.com)
  File "apps/frappe/frappe/model/document.py", line 458, in insert
    self.db_insert(ignore_if_duplicate=ignore_if_duplicate)
      self = User (john@example.com)
      ignore_permissions = True
      ignore_links = None
      ignore_if_duplicate = False
      ignore_mandatory = None
      set_name = None
      set_child_names = True
  File "apps/frappe/frappe/model/base_document.py", line 772, in db_insert
    raise frappe.DuplicateEntryError(self.doctype, self.name, e)
      self = User (john@example.com)
      ignore_if_duplicate = False
      conflict_handler = ''
      returning = ''
      d = {'name': 'john@example.com', 'owner': 'Guest', 'creation': '2026-01-24 08:59:49.826559', 'modified': '2026-01-24 08:59:49.826559', 'modified_by': 'Guest', 'docstatus': 0, 'idx': 0, 'workflow_state': None, 'enabled': 1, 'email': 'john@example.com', 'first_name': 'John', 'middle_name': None, 'last_name': 'Doe', 'full_name': 'John Doe', 'username': 'john@example.com', 'language': 'en-US', 'time_zone': 'America/New_York', 'send_welcome_email': 0, 'unsubscribed': 0, 'user_image': None, 'role_profile_name': None, 'module_profile': None, 'home_settings': None, 'gender': None, 'birth_date': None, 'interest': None, 'phone': '+17173712582', 'location': None, 'bio': None, 'mobile...us_sessions': 2, 'restrict_ip': None, 'last_ip': None, 'login_after': 0, 'user_type': 'Website User', 'last_active': None, 'login_before': 0, 'bypass_restrict_ip_check_if_2fa_enabled': 0, 'last_login': None, 'last_known_versions': None, 'api_key': None, 'api_secret': None, 'onboarding_status': '{}'}
      columns = ['name', 'owner', 'creation', 'modified', 'modified_by', 'docstatus', 'idx', 'workflow_state', 'enabled', 'email', 'first_name', 'middle_name', 'last_name', 'full_name', 'username', 'language', 'time_zone', 'send_welcome_email', 'unsubscribed', 'user_image', 'role_profile_name', 'module_profile', 'home_settings', 'gender', 'birth_date', 'interest', 'phone', 'location', 'bio', 'mobile_no', 'mute_sounds', 'desk_theme', 'code_editor_type', 'banner_image', 'search_bar', 'notifications', 'list_sidebar', 'bulk_actions', 'view_switcher', 'form_sidebar', 'form_navigation_buttons', 'timeline', 'dashboard', 'show_absolute_datetime_in_timeline', 'new_password', 'logout_all_sessions', 'reset_password...opy', 'allowed_in_mentions', 'default_workspace', 'default_app', 'simultaneous_sessions', 'restrict_ip', 'last_ip', 'login_after', 'user_type', 'last_active', 'login_before', 'bypass_restrict_ip_check_if_2fa_enabled', 'last_login', 'last_known_versions', 'api_key', 'api_secret', 'onboarding_status']
frappe.exceptions.DuplicateEntryError: ('User', 'john@example.com', IntegrityError(1062, "Duplicate entry 'john@example.com' for key 'PRIMARY'"))

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions