Skip to content

Commit

Permalink
fix(auth0): avoid use of undefined global in browser environment (#11531
Browse files Browse the repository at this point in the history
)

As part of migrating the build tool we likely removed some
transpiliation that was allowing `global` in the browser. Switching to
`globalThis` is a small change which fixes the issue.

I have tested this locally and confirmed the bug before and the fix
after.

We could also directly use `window` instead but I don't really think
there's anything more or less correct about that vs `globalThis`. I
didn't really want to go down the road to adding configuration to
esbuild to start adding back build time changes to support this.
  • Loading branch information
Josh-Walker-GM committed Sep 12, 2024
1 parent 8603343 commit 111ec71
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .changesets/11531.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- fix(auth0): avoid use of undefined global in browser environment (#11531) by @Josh-Walker-GM

The Auth0 auth provider was failing in the browser due to trying to access `global`. This change corrects this and fixes Auth0 usage in the browser.
2 changes: 1 addition & 1 deletion __fixtures__/test-project/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@
"postcss": "^8.4.45",
"postcss-loader": "^8.1.1",
"prettier-plugin-tailwindcss": "^0.5.12",
"tailwindcss": "^3.4.10"
"tailwindcss": "^3.4.11"
}
}
6 changes: 3 additions & 3 deletions packages/auth-providers/auth0/web/src/auth0.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ function createAuthImplementation(auth0Client: Auth0Client) {
client: auth0Client,
restoreAuthState: async () => {
if (
global?.location?.search?.includes('code=') &&
global?.location?.search?.includes('state=')
globalThis?.location?.search?.includes('code=') &&
globalThis?.location?.search?.includes('state=')
) {
const { appState } = await auth0Client.handleRedirectCallback()
const url = appState?.targetUrl
? appState.targetUrl
: window.location.pathname
global?.location?.assign(url)
globalThis?.location?.assign(url)
}
},
login: async (options?: RedirectLoginOptions) =>
Expand Down

0 comments on commit 111ec71

Please sign in to comment.