Skip to content

fix(auth): resolve context loss causing TypeError in secure auth methods#1273

Open
amankv1234 wants to merge 2 commits intoRocketChat:developfrom
amankv1234:default-1
Open

fix(auth): resolve context loss causing TypeError in secure auth methods#1273
amankv1234 wants to merge 2 commits intoRocketChat:developfrom
amankv1234:default-1

Conversation

@amankv1234
Copy link
Copy Markdown

Description

This PR resolves Issue #1225 where users logged in via the secure authentication flow were encountering a TypeError: Cannot read properties of undefined (reading 'handleSecureLogin') crash.

Root Cause:
In modern environments enforcing ES strict mode, this evaluates to undefined for standalone function declarations at the module level. When saveTokenSecure, getTokenSecure, and deleteTokenSecure were passed across modules into RocketChatAuth, the transpiler stripped their this bindings. Consequently, attempting to call this.handleSecureLogin within them resulted in an immediate crash.

Solution:
Refactored getTokenStorage to return the token handlers as object literal methods directly. Moving them from standalone function declarations into object methods ensures lexical scoping is preserved properly when executed within the boundaries of the RocketChatAuth instance, thus maintaining the this context correctly.

Fixes #1225

Changes Made

  • Modified packages/react/src/lib/auth.js to return object methods directly in getTokenStorage.
  • Removed top-level standalone function declarations (saveTokenSecure, etc.) that were losing context.

Steps to Test

  1. Set up EmbeddedChat with secure: true configuration.
  2. Launch the application and initiate the login flow.
  3. Observe successful authentication without console exceptions.
  4. Test the logout flow and ensure tokens are deleted reliably.

@amankv1234
Copy link
Copy Markdown
Author

Hi @7vignesh,

I have submitted this pull request with improvements related to type safety and code cleanup.
Could you please review it when you get a chance?

I would really appreciate your feedback, and if everything looks good, kindly consider merging it.

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Secure token functions crash with TypeError — users on secure auth flow cannot log in or log out

1 participant