Skip to content
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

Auth JS 6.4.0 causes issues in Angular apps when testing #1174

Closed
mraible opened this issue Apr 11, 2022 · 31 comments
Closed

Auth JS 6.4.0 causes issues in Angular apps when testing #1174

mraible opened this issue Apr 11, 2022 · 31 comments
Labels

Comments

@mraible
Copy link

mraible commented Apr 11, 2022

Describe the bug?

Today I discovered that if you upgrade an Angular app from using Auth JS 6.2.0 from 6.4.0, there are stack traces when you run npm test:

ERROR: 'Unhandled Promise rejection:', 'Failed to execute 'postMessage' on 'BroadcastChannel': Channel is closed', '; Zone:', '<root>', '; Task:', 'Promise.then', '; Value:', DOMException{stack: 'Error: Failed to execute 'postMessage' on 'BroadcastChannel': Channel is closed
    at Object.postMessage (http://localhost:9876/_karma_webpack_/webpack:/node_modules/broadcast-channel/dist/esbrowser/methods/native.js:26:1)
    at http://localhost:9876/_karma_webpack_/webpack:/node_modules/broadcast-channel/dist/esbrowser/broadcast-channel.js:198:1
    at _ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/fesm2015/zone.js:372:1)
    at ProxyZoneSpec.onInvoke (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/fesm2015/zone-testing.js:287:1)
    at _ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/fesm2015/zone.js:371:1)
    at Zone.run (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/fesm2015/zone.js:134:1)
    at http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/fesm2015/zone.js:1275:1
    at _ZoneDelegate.invokeTask (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/fesm2015/zone.js:406:1)
    at ProxyZoneSpec.onInvokeTask (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/fesm2015/zone-testing.js:318:1)
    at _ZoneDelegate.invokeTask (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/fesm2015/zone.js:405:1)'}, 'Error: Failed to execute 'postMessage' on 'BroadcastChannel': Channel is closed
    at Object.postMessage (http://localhost:9876/_karma_webpack_/webpack:/node_modules/broadcast-channel/dist/esbrowser/methods/native.js:26:1)
    at http://localhost:9876/_karma_webpack_/webpack:/node_modules/broadcast-channel/dist/esbrowser/broadcast-channel.js:198:1
    at _ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/fesm2015/zone.js:372:1)
    at ProxyZoneSpec.onInvoke (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/fesm2015/zone-testing.js:287:1)
    at _ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/fesm2015/zone.js:371:1)
    at Zone.run (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/fesm2015/zone.js:134:1)
    at http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/fesm2015/zone.js:1275:1
    at _ZoneDelegate.invokeTask (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/fesm2015/zone.js:406:1)
    at ProxyZoneSpec.onInvokeTask (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/fesm2015/zone-testing.js:318:1)
    at _ZoneDelegate.invokeTask (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/fesm2015/zone.js:405:1)'

If I revert to Auth JS 6.2.0, the problem goes away.

What is expected to happen?

Screen Shot 2022-04-11 at 14 29 38

What is the actual behavior?

Screen Shot 2022-04-11 at 14 28 55

Reproduction Steps?

git clone https://github.com/okta-samples/okta-angular-sample.git
cd okta-angular-sample
npm i && npm test

There's no stack trace because v6.2.0 is used. Update package.json to use "@okta/okta-auth-js": "^6.4.0" and try again.

npm i
npm test

You'll see the stack trace, even though tests pass.

SDK Versions

Everything works great with:

"@okta/okta-angular": "^5.1.1",
"@okta/okta-auth-js": "^6.2.0",

Execution Environment

$ node --version
v16.14.0

macOS Monterey

Additional Information?

No response

@mraible mraible added the bug label Apr 11, 2022
@mraible
Copy link
Author

mraible commented Apr 11, 2022

This seems to happen on the Vue sample as well. However, it’s in the browser console for Vue, and downgrading to Auth JS 6.2.0 doesn’t make it go away.

Screen Shot 2022-04-11 at 15 19 10

Steps to reproduce:

  1. Install the Okta CLI and configure it with your org.
  2. Run okta start vue and follow the printed instructions.
  3. Run npm start and you'll see the errors in your browser console.

@mraible
Copy link
Author

mraible commented Apr 11, 2022

This doesn't just happen in Chrome. It happens in Firefox and Safari too.

Screen Shot 2022-04-11 at 15 27 10

Screen Shot 2022-04-11 at 15 27 13

@jaredperreault-okta
Copy link
Contributor

@mraible Do you know what version of broadcast-channel is being used in your builds?

yarn why broadcast-channel should do the trick

@mraible
Copy link
Author

mraible commented Apr 11, 2022

@jaredperreault-okta I'm not using yarn, I'm using npm. I'm not sure if npm list is the same, but I tried npm list broadcast-channel in the Angular sample:

$ npm list broadcast-channel
[email protected] /Users/mraible/dev/okta-samples/okta-angular-sample
└─┬ @okta/[email protected]
  └── [email protected]

If I try it in the Vue sample, it's a similar result:

[email protected] /Users/mraible/dev/okta-samples/okta-vue-sample
└─┬ @okta/[email protected]
  └── [email protected]

@saosebastiao
Copy link

This is happening in Okta-React as well. Is there a workaround?

@jaredperreault-okta
Copy link
Contributor

@saosebastiao are you able to reproduce this with the Okta React Sample?

What version of @okta/okta-auth-js and @okta/okta-react are you using?

@shuowu
Copy link
Contributor

shuowu commented Apr 18, 2022

Fixed in 6.4.1

@shuowu shuowu closed this as completed Apr 18, 2022
@mraible
Copy link
Author

mraible commented Apr 18, 2022

I just tried validating everything is fixed using the okta-angular-sample. If I upgrade it to use Auth JS 6.4.2, the problem does go away.

Screen Shot 2022-04-18 at 10 00 12

However, if I upgrade the okta-vue-sample to 6.4.2, there are still errors in my browser's console.

Screen Shot 2022-04-18 at 09 58 24

@mraible
Copy link
Author

mraible commented Apr 18, 2022

I see something similar in the Okta CLI's React sample:

FAIL src/App.test.jsx
  ✕ renders title link (250 ms)

  ● renders title link

    No useable method found in ["native","idb","localstorage"]

      at node_modules/@okta/okta-auth-js/dist/webpack:/OktaAuth/node_modules/broadcast-channel/dist/esbrowser/method-chooser.js:42:25
      at new he (node_modules/@okta/okta-auth-js/dist/webpack:/OktaAuth/node_modules/broadcast-channel/dist/esbrowser/broadcast-channel.js:22:17)
      at e.startElector (node_modules/@okta/okta-auth-js/dist/webpack:/OktaAuth/lib/ServiceManager.ts:136:24)
      at e.start (node_modules/@okta/okta-auth-js/dist/webpack:/OktaAuth/lib/ServiceManager.ts:100:12)
      at e.value (node_modules/@okta/okta-auth-js/dist/webpack:/OktaAuth/lib/OktaAuth.ts:366:25)
      at node_modules/@okta/src/Security.tsx:83:4

By "similar", I mean broadcast-channel is in the stack trace. I tried upgrading to Auth JS 6.4.3, but it does not solve this issue.

@StephenAtCFA
Copy link

We're also seeing this in 6.4.3 when using react.

@jaredperreault-okta
Copy link
Contributor

@StephenAtCFA can you provide more details?

You're seeing this error in your application itself, or in testing (jest or otherwise)?

What versions of the other relevant modules are you using, ie React, okta-react?

Do you see this same behavior in different versions of okta-auth-js?

@shuowu shuowu reopened this Apr 18, 2022
@mrcotter
Copy link

Seeing the same error messages in the latest 6.4.3 version with my Vue 3 application using okta-auth-js and okta-signin-widget.

Downgrade okta-auth-js and lock its version to 6.2.0 works but any other new versions have similar problem.

@jaredperreault-okta
Copy link
Contributor

@mrcotter What browser/OS are you testing this on? I am currently unable to repro this issue (using okta-auth-js 6.4.3)

@StephenAtCFA
Copy link

On brave v 1.36.122 / mac os 12.3 / node 16.14.0 / react 17.0.2 / webpack 5.70.0 / @babel/core 7.17.8 / okta-react 6.4.3 / okta-auth-js 6.4.3

@StephenAtCFA
Copy link

Using redirect not okta widget

@mrcotter
Copy link

@mrcotter What browser/OS are you testing this on? I am currently unable to repro this issue (using okta-auth-js 6.4.3)

On latest versions of Chrome (100.0.4896.88) and Firefox (99.0.1) on macOS (12.3.1)

An example from the browser console:

Screen Shot 2022-04-20 at 10 00 13

@mraible
Copy link
Author

mraible commented May 2, 2022

I tried upgrading the Okta CLI's Vue sample to [email protected] today and there are still errors in my console.

Screen Shot 2022-05-02 at 12 15 10

.

@pzi
Copy link

pzi commented May 4, 2022

After login and when navigating a next.js app with okta-auth-js v6.5.0 and okta-react v6.4.3

image

@richmason
Copy link

richmason commented May 26, 2022

I'm seeing this issue in my Vue 3 project which is using @okta/okta-auth-js v6.5.1 and @okta/okta-vue v5.2.1:

CleanShot 2022-05-26 at 19 10 38

@TD-DO
Copy link

TD-DO commented Jun 9, 2022

I am seeing this in my React project:
"@okta/okta-auth-js": "^6.6.1",
"@okta/okta-react": "^6.4.3",
"@okta/okta-signin-widget": "^6.4.0",
react: 18.1.0
Is there an ETA on when this may be fixed?
Also, downgrading to 6.2.0 doesn't fix this for me
I'm seeing it in my console and it's breaking my cypress tests but not anywhere else

@TD-DO
Copy link

TD-DO commented Jun 10, 2022

Downgrading to 6.1.0 works

@jaredperreault-okta
Copy link
Contributor

okta-react 6.5.0 and okta-vue 5.3.0 were released this morning and address this issue

cc @pzi @richmason @TD-DO

@KLCarrierDirect
Copy link

KLCarrierDirect commented Jul 6, 2022

Upgrading okta-react to 6.5.0 causes the error in my React project: Can't perform a React state update on an unmounted component for working code. Downgrading to 6.1.0 and 6.2.0 do not work as we are using react-router-dom v6.2.1. Our goal in upgrading to 6.5.0 is to remove the Uncaught (in promise) DOMException: Failed to execute 'postMessage' on 'BroadcastChannel': Channel is closed errors.

Originally using the following:
"@okta/okta-auth-js": "^6.6.0",
"@okta/okta-react": "^6.4.3",
"@okta/okta-signin-widget": "^6.1.0"

@jaredperreault-okta
Copy link
Contributor

@KLCarrierDirect can you provide a snippet or an example repo which illustrates this error?

@KLCarrierDirect
Copy link

KLCarrierDirect commented Jul 6, 2022

@jaredperreault-okta

Here are the Broadcast errors using @okta/okta-react v6.4.3
Screen Shot 2022-07-06 at 11 16 45 AM

Here is the mounting error when upgrading to @okta/okta-react v6.5.0
Screen Shot 2022-07-06 at 1 45 10 PM

@jaredperreault-okta
Copy link
Contributor

jaredperreault-okta commented Jul 6, 2022

Thank you for the stack traces, but can you provide a code snippet as well? Perhaps from your ReportCards component (since it's mentioned in the stack trace). At this time I cannot reproduce this issue

@pzi
Copy link

pzi commented Jul 19, 2022

@jaredperreault-okta

okta-react 6.5.0 and okta-vue 5.3.0 were released this morning and address this issue

cc @pzi @richmason @TD-DO

Preliminary testing with our Next.js app seems to agree :) Thanks for getting this fixed. Now we just need to get the M1 issue addressed, so everyone can build the app.

@KLCarrierDirect
Copy link

KLCarrierDirect commented Jul 28, 2022

@jaredperreault-okta

Thank you for the stack traces, but can you provide a code snippet as well? Perhaps from your ReportCards component (since it's mentioned in the stack trace). At this time I cannot reproduce this issue

import Card from "@mui/material/Card"
import Grid from "@mui/material/Grid"
import Typography from "@mui/material/Typography"
import { makeStyles } from "@mui/styles"
import { useOktaAuth } from "@okta/okta-react"
import { getAdminDashReportsByReportType } from "../../adapters/moesif"

const useStyles = makeStyles((theme) => ({
    card: {
        width: "100%",
        height: 675,
        padding: "20px 20px 20px 20px",
    },
}))
const ReportsCard = ({ theme, reportType, index }) => {
    const classes = useStyles()
    const { authState } = useOktaAuth()
    const [isLoaded, setIsLoaded] = useState(false)
    const [report, setReport] = useState()```

    const token = authState?.accessToken?.accessToken

    const hideSpinner = () => setIsLoaded(true)

    useEffect(() => {
        let fetchReport

        try {
            fetchReport = async () => {
                const result = await getAdminDashReportsByReportType(
                    token,
                    reportType
                )
                setReport(result.data.reports[0])
                setIsLoaded(true)
            }

            fetchReport()
        } catch (err) {
            console.error(err)
            fetchReport = async () => {
                const result = await getAdminDashReportsByReportType(
                    token,
                    reportType
                )
                setReport(result.data.reports[0])
                setIsLoaded(true)
            }

            fetchReport()
        }
    }, [])

    return (
        <Grid item container xs={6} key={index}>
            <Card
                elevation={6}
                className={classes.card}
                // sx={{
                //     border: 1,
                //     borderColor: theme.palette.primary.dark,
                // }}
            >
                {!isLoaded ? (
                    <Grid item container zIndex={9999} justifyContent="center">
                        <Grid
                            item
                            container
                            mt="11%"
                            position="absolute"
                            justifyContent="center"
                            direction="column"
                        >
                            <Grid item justifyContent="center">
                                <Typography
                                    textAlign="center"
                                    color="#272f33"
                                    fontSize="24px"
                                >
                                    Loading. . .
                                </Typography>
                            </Grid>
                        </Grid>
                    </Grid>
                ) : null}
                {isLoaded ? (
                    <iframe
                        id={reportType}
                        src={`${report.url}&primary_color=%23396a6a&color_array=%23E37A41%2C%23396a6a%2C%23302E2C%2C%23777B83%2C%23A8A8A9&chart_font_color=%23396a6a`}
                        name={reportType}
                        frameBorder="0"
                        noresize="noresize"
                        width="100%"
                        height="100%"
                        // scrolling="no"
                        onLoad={() => hideSpinner()}
                    />
                ) : null}
            </Card>
        </Grid>
    )
}

export default ReportsCard`

---

Please let me know if this helps or if you need another code snippet. I am having difficulty replicating this issue on js fiddle.

@jaredperreault-okta
Copy link
Contributor

@KLCarrierDirect

Can you try upgrading auth-js to 6.7? 6.7 includes a fix for the channel is closed error (CHANGELOG)

I also recommend fetching tokens directly from the tokenManager rather than authState. Recently we have observed an issue with tokens on authState, we believe due to the async nature of token refresh (authState updates are slightly delayed) and we plan to remove tokens from the authState in the next major version

const ReportsCard = ({ theme, reportType, index }) => {
    const classes = useStyles()
    const { oktaAuth } = useOktaAuth()
    const [isLoaded, setIsLoaded] = useState(false)
    const [report, setReport] = useState()

    const hideSpinner = () => setIsLoaded(true)

    useEffect(() => {
        let fetchReport

        try {
            fetchReport = async () => {
                const token = await oktaAuth.tokenManager.get('accessToken');
                const result = await getAdminDashReportsByReportType(
                    token,
                    reportType
                )
                setReport(result.data.reports[0])
                setIsLoaded(true)
            }

@KLCarrierDirect
Copy link

@jaredperreault-okta

Thank you so much, upgrading to @okta/okta-auth-js 6.7.0 successfully removed the Broadcast channel errors from the console.
I will apply your recommendation to use the tokenManager as we did notice issues with our tokens when upgrading @okta/okta-react.

@jaredperreault-okta
Copy link
Contributor

Closing this issue since the original report of the channel is closed error has been fixed in auth-js 6.7

A few other discussions have spawned on this thread, please open tickets if you need additional assistance

Jimoou added a commit to Jimoou/BookLibrarySite-Backend that referenced this issue Mar 9, 2023
Okta-react 사용중 console에 BroadCastChannel 에러가 계속 떠서, okta-auth-js를 6.7.0으로 업데이트 하면서 오류 해결함.

참고
okta/okta-auth-js#1174
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

10 participants