Function Redirect to another page

Hello, i need to redirect if the user onclick, the function verify if is good and redirect

const auth = async () => {
        let token = false
        await isToken()
            .then(response => {
                token = response
            })
        if (token) {
            return (
                <IonRouterOutlet>
                    <Route path="/events" component={Events} exact={true}/>
                    <Route path="/" render={() => <Redirect to="/events"/>} exact={true}/>
                </IonRouterOutlet>
            )
        }
    }

return (<IonButton onClick={(e) => auth()}>teste redirect</IonButton>)

my code doesn’t work, if you have any documentation because I haven’t found much
or maybe I missed a fundamental principle in the architecture of the code.

1 Like

you have missed a fundamental issue…

See react-router documentation: https://reacttraining.com/react-router/web/example/auth-workflow
Here is an working implementation in Ionic, there are differences in the ionic-router impementation, specifically around the use of <Switch>

App with routing setup - https://github.com/aaronksaunders/ionic-sidemenu-tabs-auth-starter/blob/8cb5984ae8a0a5cad02c83cb4293a8edc8cee7fb/src/App.tsx
Login Page - https://github.com/aaronksaunders/ionic-sidemenu-tabs-auth-starter/blob/8cb5984ae8a0a5cad02c83cb4293a8edc8cee7fb/src/pages/auth/LoginPage.tsx

2 Likes