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

Unhandled Rejection (TypeError): Object(...) is not a function #64

Closed
sumitka194 opened this issue Dec 17, 2020 · 12 comments
Closed

Unhandled Rejection (TypeError): Object(...) is not a function #64

sumitka194 opened this issue Dec 17, 2020 · 12 comments

Comments

@sumitka194
Copy link

sumitka194 commented Dec 17, 2020

image

Getting this error while loading the app first time after login redirect, but when manually try to remove the implicit/callback and reload the application it loads fine

After investigating I found out that the issue might be in

if (!oktaAuth.options.restoreOriginalUri) {
this condition as it attaches the default restoreOriginalUri after the first load, that's why it works fine after removing the implicit/callback manually, also if I add restoreOriginalUri method in OktaAuth then it is working fine

Should we keep this

if (!oktaAuth.options.restoreOriginalUri) {
condition out of useEffect as well, not sure if there is some other logic behind keeping it in useEffect only

@okta/okta-auth-js: 4.2.0
@okta/okta-react: 4.0.0

Thanks

@oleksandrpravosudko-okta
Copy link
Contributor

oleksandrpravosudko-okta commented Dec 17, 2020

thanks for submission @sumit194

I wasn't able to reproduce this error using barebones app w/ specified package versions. Can you post more details on auth API you are using(e.g. signInWithRedirect) + your router configuration?

@shuowu
Copy link
Contributor

shuowu commented Dec 17, 2020

@sumit194 The default restoreOriginalUri callback function uses history API from react-router-dom, have you included the module in your dependencies?

You can also override the default implementation by following this guide.

@sumitka194
Copy link
Author

Hi @oleksandrpravosudko-okta below are the implementation of my Okta Auth component

const ProtectedRoute = ({ children, ...rest }) => (
  <Suspense fallback={<div>loading...</div>}>
    <OktaComponent {...rest}>{children}</OktaComponent>
  </Suspense>
);

const OktaProvider = (props) => {
  const history = useHistory();
  const oktaAuth = new OktaAuth({
    issuer: config.okta.issuer,
    clientId: config.okta.client_id,
    redirectUri: config.okta.redirect_uri,
    restoreOriginalUri: async (_, originalUri) => {
      history.replace(toRelativeUrl(originalUri, window.location.origin));
    },
  });
  return (
    <Security oktaAuth={oktaAuth}>
      <SecureRoute path="/">
        <ProtectedRoute {...props} />
      </SecureRoute>
      <Route path="/implicit/callback">
        <LoginCallback />
      </Route>
    </Security>
  );
};

Now it is working fine, but if I remove the restoreOriginalUri from the oktaAuth then it throws the above error in first load

@shuowu yes I have react-router-dom as a dependency in my project, and after overriding the restoreOriginalUrl method it is working fine, but that should work without overriding as well

@shuowu
Copy link
Contributor

shuowu commented Dec 21, 2020

@sumit194 Can you try to move the oktaAuth initialization outside of the OktaProvider? As it may end up with creating more than one oktaAuth instances, which is not expected and may cause weird errors.

@sumitka194
Copy link
Author

@shuowu before it was like that only, but after getting this issue I have to move it inside the component to use the history in restoreOriginalUrl

@shuowu
Copy link
Contributor

shuowu commented Dec 21, 2020

@sumit194 The error looks like a module export issue, but I cannot tell how it relevant with override the restoreOriginalUrl callback. Can you try our official sample to see if you can repro the issue there?

@sumitka194
Copy link
Author

@shuowu sure, I'll try that, also can you please explain more about the module export issue (any specific component at okta package side or project side)

@shuowu
Copy link
Contributor

shuowu commented Dec 22, 2020

@sumit194 I saw some related issues by searching the error (facebook/react#14484). You can check the details in the linked thread.

@jchabotamica
Copy link

I believe @sumit194 is referring to circular dependencies with exports/imports.

@radicand
Copy link

radicand commented Mar 1, 2021

For anyone still tracking this, I had this issue as well, and it was because my <Security> block was outside the <Router> block. After nesting <Security> inside <Router> the issue went away.

@shuowu
Copy link
Contributor

shuowu commented Jun 17, 2021

@sumit194 Does @radicand 's comment above help?

Also, the dependency of react-router has been removed from the <Security> component since version 5. You can also try migrate to the latest version, see the migration guide for details.

@shuowu
Copy link
Contributor

shuowu commented Jun 17, 2021

Close the issue for now, feel free to reopen it if the issue still exist.

@shuowu shuowu closed this as completed Jun 17, 2021
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

No branches or pull requests

5 participants