oidc-client: useOidcUser briefly returning null on page navigation refresh during authenticated session [NextJs]
Issue and Steps to Reproduce
Create a starter NextJs app (choose “no” to App router, “yes” tosrc/):
npx create-next-app
Modify src/pages/index.tsx to:
import { OidcSecure, useOidcUser } from "@axa-fr/react-oidc";
const isBrowser = () => typeof window !== "undefined";
export default function Home() {
const { oidcUser } = useOidcUser();
console.log("isBrowser: " + isBrowser() + ", oidcUser: ");
console.log(oidcUser);
return (
<div>
Anyone can see this
<OidcSecure>
<div className="text-red-400">
Not everybody can see this.... {oidcUser?.email}
</div>
</OidcSecure>
</div>
);
}
Modify src/pages/_app.tsx:
import "@/styles/globals.css";
import { OidcConfiguration, OidcProvider } from "@axa-fr/react-oidc";
import type { AppProps } from "next/app";
const oidcConfig: OidcConfiguration = {
authority: <snip>,
client_id: <snip>,
service_worker_only: false,
scope: "openid profile email offline_access",
redirect_uri: "http://localhost:3000/ibor/callback",
};
export default function App({ Component, pageProps }: AppProps) {
return (
<OidcProvider configuration={oidcConfig}>
<Component {...pageProps} />
</OidcProvider>
);
}
Add a callback route src/pages/ibor/callback/index.tsx:
import React from "react";
import { useOidc } from "@axa-fr/react-oidc";
import { useRouter } from "next/router";
const Page = () => {
const { isAuthenticated } = useOidc();
const router = useRouter();
if (isAuthenticated) {
router.push("/");
} else router.push("/signin");
return <div>Loading...</div>;
};
export default Page;
Versions
7.22.3 (I also tried the alpha version mentioned here: https://github.com/AxaFrance/oidc-client/issues/1090, with the new config option, but had no success there)
Screenshots
Expected
useOidcUser always returns the user object when user is authenticated (and we’re not in an SSR context)
Actual
After authenticating, you’ll see:
…and in the console:
I had at first assumed the reason would be that useOidcUser was getting called during Server Side Rendering… but as you can see, that’s not the cause (note the isBrowser check). So local / session storage should be available at that time.
If you refresh the page, or navigate to another route, you’ll see the same thing.
The reason this matters is that if you’re taking, say, the name or email address of the logged in user and displaying them in a header, they will flicker and disappear before reappearing whenever the user navigates / refreshes. There are workarounds (you can cache it in some state somewhere) - but that’s obviously not ideal.
Additional Details
- Installed packages:
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 17 (7 by maintainers)
Works like a charm. You can close this. Thanks a lot!
Hi @attiqeurrehman thank you for your feedback again. I will investigate it after my holiday near the 13 may.
Thank to all your details I think it will be easy to reproduce and debug.
I have another piece of information, maybe it will help. right after the
/tokencall returns with valid tokens, there is a call to the/userinfoendpoint which fails because noauthorizationheader is supplied. Then a second/userinfocall is made right after which has theauthorizationheader and thus succeds:Here is that failed userinfo call as a CURL:
(this is with
preload_user_info: true,in the config)@guillaume-chervet no worries, hope you had a good time.
@guillaume-chervet I’m experiencing similar issue where after login, the
oidcUserobject is initiallynull. Additionally, the subsequent call to theuserinfoendpoint returns an unauthorized error. There is no additional call and have to refresh the page and then everything works fine.Following is the configuration:
Version: 7.22.4
@guillaume-chervet
You can clone this: https://github.com/kramer99/oidc-test
…update
_app.tsxwith your IDP details and run. You should see the problem. I’ve tried it with two different IDPs (Okta and Google Cloud).Confirmed, fix looks good on my side.
Thanks for the quick work @guillaume-chervet !
Hi @kramer99 thank you for your issue and feedback. I will make more test to debug and enhance it !