I’m working on an Ionic app with React. Everything is working on in localhost web, I can login and the app is returning to /callback
after login.
Then, when I compiled and build the apk and install on my Android device I have this problem that after a successfull login, the universal login closes, but instead of redirect me to /callback
seems to go back to ‘/’ without authenticate the current user.
index.tsx
import React from "react";
import { createRoot } from "react-dom/client";
import { Auth0Provider } from "@auth0/auth0-react";
import App from "./App";
import * as serviceWorkerRegistration from "./serviceWorkerRegistration";
import reportWebVitals from "./reportWebVitals";
import { domain as auth0Domain, clientId, callbackUri } from "./auth.config";
const root = createRoot(document.getElementById("root") as HTMLElement);
root.render(
<React.StrictMode>
<Auth0Provider
domain={auth0Domain}
clientId={clientId}
authorizationParams={{
redirect_uri: callbackUri
}}
// For using Auth0-React with Ionic on Android and iOS,
// it's important to use refresh tokens without the fallback
useRefreshTokens={true}
useRefreshTokensFallback={false}
>
<App />
</Auth0Provider>
</React.StrictMode>
);
< App />
const App: React.FC = () => {
const { handleRedirectCallback } = useAuth0();
const history = useHistory()
useEffect(() => {
CapApp.addListener("appUrlOpen", async ({ url }) => {
if (url.startsWith(callbackUri)) {
if (
url.includes("state") &&
(url.includes("code") || url.includes("error"))
) {
await handleRedirectCallback(url);
}
await Browser.close();
}
});
}, [handleRedirectCallback, history]);
return (
<IonApp>
<IonReactRouter>
<AppRoutes />
</IonReactRouter>
</IonApp>
);
};
< AppRoutes />
export default function AppRoutes() {
const { isAuthenticated } = useAuth0()
return (
<IonRouterOutlet>
< Route exact={true} path="/" render={() => isAuthenticated
? <Redirect
from="/"
to='/callback'
/>
: <Login />
}
/>
< Route exact={true} path="/callback" render={() => <Callback />} />
< CampusRoutes />
</IonRouterOutlet>
)
}
capacitor.config.ts
import { CapacitorConfig } from "@capacitor/cli";
const config: CapacitorConfig = {
appId: "app.id.name",
appName: "Forvet Campus",
webDir: "build",
plugins: {
CapacitorAuth0Plugin: {
clientId: 'true_client_id',
domain: 'true_domain',
scheme: 'app.id.name',
redirectUri: 'app.id.name://true_domain/capacitor/app.id.name/callback',
},
},
};
export default config;
< LoginButton />
import { useAuth0 } from "@auth0/auth0-react";
import { Browser } from "@capacitor/browser";
import { IonButton } from "@ionic/react";
const LoginButton: React.FC = () => {
const { loginWithRedirect } = useAuth0();
const login = async () => {
await loginWithRedirect({
async openUrl(url) {
await Browser.open({
url,
windowName: "_self"
});
}
});
};
return <IonButton color='secondary' onClick={login}>Ingresar</IonButton>;
};
export default LoginButton;
Callback URLs and logout on Auth0 are set correctly
I also add on AndroidManifest.xml <intent-filter>
with attribute