Pending state is not changing even after the data is available.

const { data, isPending } = authClient.useListOrganizations.get(); data is not being available until I soft refresh the page (Next.js) and the isPending is not returning false even after the data is fetched. Is this a bug or is it something wrong with my implementation?
15 Replies
bekacru
bekacru•4w ago
make sure you import the client from /react
import { createAuthClient } from "better-auth/react"
import { createAuthClient } from "better-auth/react"
Muhammad Isa
Muhammad IsaOP•4w ago
Ya, I've imported it from "better-auth/react"
joseph013318
joseph013318•5d ago
Hey @Muhammad Isa have you found the solution?
Muhammad Isa
Muhammad IsaOP•5d ago
Nah, I stopped using better-auth after that. I implemented my own auth using Lucia Auth guide.
bekacru
bekacru•5d ago
could you share me something I can reproduce? or at least if you can record screen of the behavior. I couldn't replicate it.
bekacru
bekacru•5d ago
this is running on the client using isPending to show loading icons once the user sign in, and when pending is false it shows the session.
function SessionComp() {
const { data, isPending, error } = client.useSession();
return (
<div>
{isPending ? (
<div>
<Loader2 />
</div>
) : error ? (
<div>
Error: {error.message}
</div>
) : session ? (
<div></div>
) : (
<div> </div>
)}
</div>
);
}
function SessionComp() {
const { data, isPending, error } = client.useSession();
return (
<div>
{isPending ? (
<div>
<Loader2 />
</div>
) : error ? (
<div>
Error: {error.message}
</div>
) : session ? (
<div></div>
) : (
<div> </div>
)}
</div>
);
}
joseph013318
joseph013318•5d ago
@bekacru It's happening for me using Oauth and in worst prossible time almost going in production 😭, the loader is infinite sometimes. I will reproduce and share that same with you!
bekacru
bekacru•5d ago
why do you need to use isPending for oauth? Since there will be a redirect, the session would need to load in another page or at least the current page needs to reload.
joseph013318
joseph013318•5d ago
In my navbar I am showing login button. When login is done in place of login button the profile card will appear so in mean time the loader is shown. import { authClient } from "@/lib/auth-client"; import { useEffect, useRef } from "react"; function useBetterAuthSession() { const { data, isPending, error, //error object refetch, } = authClient.useSession(); // Track the latest value of isPending const isPendingRef = useRef(isPending); useEffect(() => { isPendingRef.current = isPending; }, [isPending]); useEffect(() => { if (isPending) { const timerNotify = setTimeout(() => { if (isPendingRef.current) { refetch(); // sends another get-session request to the server //authClient.$store.notify("$sessionSignal"); // or this poke store to trigger a re-render } }, 2500); // after 2.5 seconds of pending, force resetting client store const timerReload = setTimeout(() => { if (isPendingRef.current) { window.location.reload(); } }, 5000); // after 5 seconds of pending, reload the page return () => { clearTimeout(timerNotify); clearTimeout(timerReload); }; } }, [isPending]); return { data, isPending, error, }; } export default useBetterAuthSession; In mean time this worked, Someone suggested on github. (https://github.com/better-auth/better-auth/issues/1006#:~:text=last%20week-,I%20have%20solution,-.)
bekacru
bekacru•5d ago
so you need to show loading on your nav bar until the session is fetched and currently the isPending state is always true, is that right?
joseph013318
joseph013318•5d ago
Exactly!
bekacru
bekacru•5d ago
hmm. so, if you reload your site when there is a session already, does the isPending stays true indefinitely?
joseph013318
joseph013318•5d ago
Yes, literally searched two days for the solution but couldn't find until I came across the above code. I don't wanna switch the auth I just love better-auth. I will try to reproduce the issue and share the code!
bekacru
bekacru•5d ago
Yeah, I couldn't replicate that. For the record, I'm currently trying it on 1.2.0-beta.16. Update if you're not on the 1.2 beta, although I don't think there are any changes that would affect this behavior.
joseph013318
joseph013318•5d ago
Okay will try that! Thanks anyway!

Did you find this page helpful?