So I’m using two libs in my app:
@auth0/auth0-react
auth0-js
My React.js app is wrapped around the Auth0Provider
component, like so:
import React from 'react';
import { useHistory } from 'react-router-dom';
import { Auth0Provider } from '@auth0/auth0-react';
const Auth0ProviderWithHistory = ({ children }) => {
const history = useHistory();
const onRedirectCallback = (appState) =>
history.push(appState?.returnTo || window.location.pathname);
return (
<Auth0Provider
domain={process.env.NX_AUTH0_DOMAIN}
clientId={process.env.NX_AUTH0_CLIENT_ID}
redirectUri={window.location.origin}
onRedirectCallback={onRedirectCallback}
audience={process.env.NX_AUTH0_AUDIENCE}
scope="openid profile email"
>
{children}
</Auth0Provider>
);
};
export default Auth0ProviderWithHistory;
I have a custom form to handle login without navigating outside the page which is working ok.
import auth0 from 'auth0-js';
...
const handleLogin = (data) => {
const webAuth = new auth0.WebAuth({
domain: process.env.NX_AUTH0_DOMAIN,
clientID: process.env.NX_AUTH0_CLIENT_ID,
audience: process.env.NX_AUTH0_AUDIENCE,
redirectUri: window.location.origin,
scope: 'openid profile email',
responseType: 'code',
});
return webAuth.login(
{
username: data.email,
password: data.password,
popup: true,
},
() => history.push(successfulLoginRedirectUrl),
);
};
The issue I am struggling with is that auth0-js
does not set a cache/session in @auth0/auth0-react
/Auth0ProviderWithHistory
…
I think that is might not be possible to authenticate user on auth0-react with auth0.js so maybe it is somehow possible with spa-js
?
How to properly login a user by passing email/password with @auth0/auth0-react
or @auth0/auth0-spa-js
or auth0-js
?