-
Notifications
You must be signed in to change notification settings - Fork 86
Open
Description
I try to use react-oidc-context in a small example project. Most of it based on the code of this README.md.
It seems like the onSigninCallback is not working properly and I need a second login to get the correct state, token and user info.
This is the behavior
- click login button (call signinRedirect() )
- redirected to Keycloak
- login with my username/password
- redirected back to my app and path /oauth2/callback + parameters in URL
- ERROR: "No matching state found in storage"
- (at this time, the onSignInCallback was not triggered!)
- click on the login button again (call signinRedirect(), exactly like the first time)
- without getting redirected a second time to Keycloak, onSigninCallback is triggered and I am logged in!
keycloakConfig.tsx
export const signinCallback = (_user: User | void): void => {
console.log("onSigninCallback");
console.log(_user);
window.history.replaceState(
{},
document.title,
window.location.pathname
)
}
export const oidcConfig:AuthProviderProps = {
authority: "http://127.0.0.1:7080/realms/test",
client_id: "frontend",
// This redirect URI needs to match exactly your configuration in Keycloak!
//redirect_uri: "http://127.0.0.1:5173",
redirect_uri: "http://127.0.0.1:5173/oauth2/callback",
onSigninCallback: signinCallback
};
main.tsx
import { AuthProvider } from 'react-oidc-context';
import { oidcConfig } from './keycloakConfig.tsx';
createRoot(document.getElementById('root')!).render(
<StrictMode>
<AuthProvider {...oidcConfig}>
<App />
</AuthProvider>
</StrictMode>,
)
App.tsx
import { useAuth } from 'react-oidc-context';
function App() {
const {isAuthenticated, isLoading, error, user, settings, activeNavigator, removeUser, signinRedirect} = useAuth();
console.log(settings);
switch (activeNavigator) {
case "signinSilent":
return <div>Signing you in...</div>;
case "signoutRedirect":
return <div>Signing you out...</div>;
}
if (isLoading) {
return <div>Loading...</div>;
}
if (isAuthenticated) {
return (
<div>
Hello {user?.profile.sub}{" "}
<p>{JSON.stringify(user)}</p>
<button onClick={() => void removeUser()}>Log out</button>
</div>
);
}
return (
<div>
{(error ? <div>Oops... {error.message}</div> : <></>)}
<button onClick={() => void signinRedirect()}>Log in</button>
</div>
);
}
Metadata
Metadata
Assignees
Labels
No labels