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

Login router problem #113

Closed
aolmez opened this issue Sep 10, 2018 · 8 comments
Closed

Login router problem #113

aolmez opened this issue Sep 10, 2018 · 8 comments

Comments

@aolmez
Copy link

aolmez commented Sep 10, 2018

Hi

i want to route login page for opening page. How can make it ? i change router but return empty page.
can you help me ?

    <HashRouter>
        <Switch>
          <Route exact path="/register" name="Register Page" component={Register} />
          <Route exact path="/404" name="Page 404" component={Page404} />
          <Route exact path="/500" name="Page 500" component={Page500} />
          <Route path="/home" name="Home" component={DefaultLayout} />
          <Route  path="/login" name="Login Page" component={Login} />
        </Switch>
      </HashRouter>
@jeff-nz
Copy link

jeff-nz commented Sep 25, 2018

Hi @aolmez ,

Are you making it required for people to login before they can see contents in DefaultLayout component ?

If so, here's some code:

const isAuthenticated = () => {
  //write your condition here
  return false;
}


const UnauthenticatedRoute = ({ component: Component, ...rest }) => (
  <Route {...rest} render={(props) => (
    !isAuthenticated()
      ? <Component {...props} />
      : <Redirect to='/' />
  )} />
);


const AuthenticatedRoute = ({ component: Component, ...rest }) => (
  <Route {...rest} render={(props) => (
    isAuthenticated()
      ? <Component {...props} />
      : <Redirect to='/login' />
  )} />
);


class App extends Component {
  render() {
    return (
        <HashRouter>
          <Switch>
            <UnauthenticatedRoute exact path="/login" name="Login Page" component={Login} />
            <Route exact path="/register" name="Register Page" component={Register} />
            <Route exact path="/404" name="Page 404" component={Page404} />
            <Route exact path="/500" name="Page 500" component={Page500} />
            <AuthenticatedRoute path="/" name="Home" component={DefaultLayout} />
          </Switch>
        </HashRouter>
    );
  }
}

I hope it helps.

@aolmez
Copy link
Author

aolmez commented Sep 25, 2018 via email

@noeljoaquin30
Copy link

I tried that using above scenario but after click login didn't redirect to homepage, my laptop's memory keep increasing and the browser suddenly turn unresposive page

@MoradiGit
Copy link

<HashRouter>
    <Switch>
      <Route exact path="/register" name="Register Page" component={Register} />
      <Route exact path="/404" name="Page 404" component={Page404} />
      <Route exact path="/500" name="Page 500" component={Page500} />
      <Route exact path="/login" name="Login Page" component={Login} />
      <Route path="/home" name="Home" component={DefaultLayout} />
    </Switch>
  </HashRouter>

its worked man

@mitchbregs
Copy link

mitchbregs commented Apr 22, 2020

Thanks so much for this! @jeff-nz - would you advise to make all the non protected Route become UnauthenticatedRoute as such in this scenario? To illustrate:

      <HashRouter>
          <React.Suspense fallback={loading()}>
            <Switch>
              <UnauthenticatedRoute exact path="/login" name="Login Page" component={Login} />
              <UnauthenticatedRoute exact path="/register" name="Register Page" component={Register} />
              <UnauthenticatedRoute exact path="/404" name="Page 404" component={Page404} />
              <UnauthenticatedRoute exact path="/500" name="Page 500" component={Page500} />
              <AuthenticatedRoute path="/" name="Home" component={DefaultLayout} />
            </Switch>
          </React.Suspense>
      </HashRouter>

@woothu woothu closed this as completed Jun 25, 2020
@arindamad
Copy link

Hi @aolmez ,

Are you making it required for people to login before they can see contents in DefaultLayout component ?

If so, here's some code:

const isAuthenticated = () => {
  //write your condition here
  return false;
}


const UnauthenticatedRoute = ({ component: Component, ...rest }) => (
  <Route {...rest} render={(props) => (
    !isAuthenticated()
      ? <Component {...props} />
      : <Redirect to='/' />
  )} />
);


const AuthenticatedRoute = ({ component: Component, ...rest }) => (
  <Route {...rest} render={(props) => (
    isAuthenticated()
      ? <Component {...props} />
      : <Redirect to='/login' />
  )} />
);


class App extends Component {
  render() {
    return (
        <HashRouter>
          <Switch>
            <UnauthenticatedRoute exact path="/login" name="Login Page" component={Login} />
            <Route exact path="/register" name="Register Page" component={Register} />
            <Route exact path="/404" name="Page 404" component={Page404} />
            <Route exact path="/500" name="Page 500" component={Page500} />
            <AuthenticatedRoute path="/" name="Home" component={DefaultLayout} />
          </Switch>
        </HashRouter>
    );
  }
}

I hope it helps.

getting error in prop type

@DIBO237
Copy link

DIBO237 commented Apr 3, 2023

[UnauthenticatedRoute] is not a component. All component children of must be a or <React.Fragment>

Hi @aolmez ,

Are you making it required for people to login before they can see contents in DefaultLayout component ?

If so, here's some code:

const isAuthenticated = () => {
  //write your condition here
  return false;
}


const UnauthenticatedRoute = ({ component: Component, ...rest }) => (
  <Route {...rest} render={(props) => (
    !isAuthenticated()
      ? <Component {...props} />
      : <Redirect to='/' />
  )} />
);


const AuthenticatedRoute = ({ component: Component, ...rest }) => (
  <Route {...rest} render={(props) => (
    isAuthenticated()
      ? <Component {...props} />
      : <Redirect to='/login' />
  )} />
);


class App extends Component {
  render() {
    return (
        <HashRouter>
          <Switch>
            <UnauthenticatedRoute exact path="/login" name="Login Page" component={Login} />
            <Route exact path="/register" name="Register Page" component={Register} />
            <Route exact path="/404" name="Page 404" component={Page404} />
            <Route exact path="/500" name="Page 500" component={Page500} />
            <AuthenticatedRoute path="/" name="Home" component={DefaultLayout} />
          </Switch>
        </HashRouter>
    );
  }
}

I hope it helps.

Hi gettting this error

@MHZarei
Copy link

MHZarei commented Apr 4, 2023

I have same error, Redirect is removed from 'react-router-dom'.
https://stackoverflow.com/a/69408107/1490377

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

9 participants