Skip to content

fix: prioritize setAuth action #1435

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

filipecabaco
Copy link
Member

What kind of change does this PR introduce?

Prioritizes setAuth action in the event loop

filipecabaco and others added 3 commits May 23, 2025 13:17
* test: run integration tests in CI

* run test:ingration twice

* Install deno on integration job

* use npm

* run puppeteer with no sandbox

* run browser integration test twice

* trying to fix umd build command

* no need to run browser integration twice
@filipecabaco filipecabaco requested a review from Copilot June 2, 2025 03:15
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR aims to prioritize the setAuth action within the event loop by converting authentication event handling to an async model.

  • Converted _listenForAuthEvents and _handleTokenChanged to async functions
  • Introduced a setTimeout to defer the _handleTokenChanged call and updated realtime.setAuth to be awaited

private async _listenForAuthEvents() {
return await this.auth.onAuthStateChange((event, session) => {
setTimeout(
async () => await this._handleTokenChanged(event, 'CLIENT', session?.access_token),
Copy link
Preview

Copilot AI Jun 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider wrapping the async callback in a try/catch block inside the setTimeout to handle potential errors from _handleTokenChanged, preventing unhandled promise rejections.

Suggested change
async () => await this._handleTokenChanged(event, 'CLIENT', session?.access_token),
async () => {
try {
await this._handleTokenChanged(event, 'CLIENT', session?.access_token)
} catch (error) {
console.error('Error in _handleTokenChanged:', error)
}
},

Copilot uses AI. Check for mistakes.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since the other suggested change is also try/catching inside the _handleTokenChanged, don't need to catch it here also.

@@ -339,7 +341,8 @@ export default class SupabaseClient<
) {
this.changedAccessToken = token
} else if (event === 'SIGNED_OUT') {
this.realtime.setAuth()
await this.realtime.setAuth()
Copy link
Preview

Copilot AI Jun 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure that any potential failure from realtime.setAuth is handled, for example by adding error handling or logging in case the operation fails.

Suggested change
await this.realtime.setAuth()
try {
await this.realtime.setAuth()
} catch (error) {
console.error('Failed to set auth for realtime client:', error)
}

Copilot uses AI. Check for mistakes.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this case I think the most correct would be:

try {
      await this.realtime.setAuth()

      if (source == 'STORAGE') this.auth.signOut()
} catch (error) {
      console.log('Failed to set auth for realtime client:', error)
} finally {
      this.changedAccessToken = undefined
}

wdyt @filipecabaco ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably yes as it would also prevent running an old token against it... but will this break auth?

@coveralls
Copy link

Pull Request Test Coverage Report for Build 15382986746

Details

  • 2 of 4 (50.0%) changed or added relevant lines in 1 file are covered.
  • 2 unchanged lines in 1 file lost coverage.
  • Overall coverage decreased (-3.6%) to 69.333%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/SupabaseClient.ts 2 4 50.0%
Files with Coverage Reduction New Missed Lines %
src/SupabaseClient.ts 2 66.67%
Totals Coverage Status
Change from base Build 15323691454: -3.6%
Covered Lines: 106
Relevant Lines: 132

💛 - Coveralls

@filipecabaco filipecabaco changed the base branch from next to master June 2, 2025 10:36
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.

3 participants