Giter Club home page Giter Club logo

Comments (4)

frederikprijck avatar frederikprijck commented on June 2, 2024

Hello,

You should be able to set appState.target when calling loginWithRedirect or using withAuthenticationRequired, that is what's used to redirect back to a specific route after handeling the callback.

Let me know if that doesnt help.

from auth0-react-samples.

ara-vardanyan avatar ara-vardanyan commented on June 2, 2024

Hello,

You should be able to set appState.target when calling loginWithRedirect or using withAuthenticationRequired, that is what's used to redirect back to a specific route after handeling the callback.

Let me know if that doesnt help.

Thanks for the quick response! Still encountering the issue of being redirected to the home url. I have made sure to add all necessary callback urls in the application settings.

// client/src/main.jsx
import { Auth0Provider } from '@auth0/auth0-react';
import { auth0_domain, auth0_client_id, auth0_audience } from '../config';

ReactDOM.createRoot(document.getElementById('root')).render(
  <BrowserRouter>
    <Auth0Provider
      domain={auth0_domain}
      clientId={auth0_client_id}
      authorizationParams={{
        redirect_uri: window.location.origin,
      }}
      // audience={auth0_audience}
    >
        <App />
    </Auth0Provider>
  </BrowserRouter>
);
// client/src/components/LoginButton.jsx
import React, { useContext } from 'react';
import { useAuth0 } from '@auth0/auth0-react';
import { AppContext } from '../contexts/AppContext';

const LoginButton = () => {
  const { loginWithRedirect } = useAuth0();
  const { isAuthenticated } = useContext(AppContext);
  const targetPath = window.location.href;

  const handleClick = () => {
    console.log(window.location.href);
    setTimeout(() => {
      loginWithRedirect({ appState: { returnTo: window.location.targetPath } });
    }, 2000); // delay the redirect by 2 seconds so we can log the path (2000 milliseconds)
  };

  return (
    !isAuthenticated && (
      <button onClick={handleClick}>
        Log In
      </button>
    )
  );
};

export default LoginButton;

Does the redirectUri specified in the Auth0Provider take precedence over uri specified in the appState.returnTo in the loginWithRedirect function? Really appreciate your help with this issue!

from auth0-react-samples.

frederikprijck avatar frederikprijck commented on June 2, 2024

You need appState.target, not appState.returnTo.

Would that work ?

from auth0-react-samples.

ara-vardanyan avatar ara-vardanyan commented on June 2, 2024

You need appState.target, not appState.returnTo.

Would that work ?

That didn't work. What did work however was changing the redirect_uri in the Auth0Provider to window.location.href. Thanks for your help!

from auth0-react-samples.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.