isLoading indicator on all the hooks on the auth client #77

Closed
opened 2026-03-13 07:31:53 -05:00 by GiteaMirror · 5 comments
Owner

Originally created by @firstaxel on GitHub (Oct 15, 2024).

i am making a request to add a loading indicator on the signUp and signIn hooks cause it helps the user experience when loading indicator is shown when any functions is run on the client

Originally created by @firstaxel on GitHub (Oct 15, 2024). i am making a request to add a loading indicator on the signUp and signIn hooks cause it helps the user experience when loading indicator is shown when any functions is run on the client
Author
Owner

@Loa212 commented on GitHub (Oct 15, 2024):

Since those are just promises (not hooks), tanstack-query's useMutation can achieve the same result.

No need to add it here IMHO.

Here's how I would do it:

import { useMutation } from '@tanstack/react-query';
import { signIn } from '@/lib/auth-client';

const useSignIn = () => {
  return useMutation({
    mutationFn: async (credentials: { email: string; password: string }) => {
      return await signIn(credentials);
    },
  });
};

// Usage in a component
const SignInComponent: React.FC = () => {
  const { mutate: signIn, isLoading, isError, error } = useSignIn();

  const handleSignIn = () => {
    signIn({ email: 'user@example.com', password: 'password' });
  };

  return (
    <button onClick={handleSignIn} disabled={isLoading}>
      {isLoading ? 'Signing In...' : 'Sign In'}
    </button>
  );
};

TLDR: use tanstack

@Loa212 commented on GitHub (Oct 15, 2024): Since those are just promises (not hooks), tanstack-query's useMutation can achieve the same result. No need to add it here IMHO. Here's how I would do it: ```tsx import { useMutation } from '@tanstack/react-query'; import { signIn } from '@/lib/auth-client'; const useSignIn = () => { return useMutation({ mutationFn: async (credentials: { email: string; password: string }) => { return await signIn(credentials); }, }); }; // Usage in a component const SignInComponent: React.FC = () => { const { mutate: signIn, isLoading, isError, error } = useSignIn(); const handleSignIn = () => { signIn({ email: 'user@example.com', password: 'password' }); }; return ( <button onClick={handleSignIn} disabled={isLoading}> {isLoading ? 'Signing In...' : 'Sign In'} </button> ); }; ``` TLDR: use tanstack
Author
Owner

@Bekacru commented on GitHub (Oct 15, 2024):

you can do what @Loa212 recommended here or you can also have your own loading state and set it on a callback.

const [loading, setLoading] = useState(false)

await signIn.email(
     {
         email,
         password
     },
     {
          onResponse: () => {
                setLoading(false);
           },
          onRequest: () => {
                 setLoading(true);
          },
          onError: (ctx) => {
                toast.error(ctx.error.message);
          },
      }
 )
@Bekacru commented on GitHub (Oct 15, 2024): you can do what @Loa212 recommended here or you can also have your own loading state and set it on a callback. ```ts const [loading, setLoading] = useState(false) await signIn.email( { email, password }, { onResponse: () => { setLoading(false); }, onRequest: () => { setLoading(true); }, onError: (ctx) => { toast.error(ctx.error.message); }, } ) ```
Author
Owner

@firstaxel commented on GitHub (Oct 15, 2024):

I tried the tanstack approach but there is a issue associated with that ,
which is you can't do the same with socials cause it need callbackurl which
will not be redirected with tansack query and also with email, there is a
issue with when signing up with email it is complicated . the way I will
use Bereket way but having to tackle with usestate, I just wish we could
have the same time done like tansack query which we have the mutate,
isloading, data, error it will be a good developer experience and also for
the user

On Tue, Oct 15, 2024, 5:39 PM Bereket Engida @.***>
wrote:

you can do what @Loa212 https://github.com/Loa212 recommended here or
you can also have your own loading state and set it on a callback.

const [loading, setLoading] = useState(false)
await signIn.email(
{
email,
password
},
{
onResponse: () => {
setLoading(false);
},
onRequest: () => {
setLoading(true);
},
onError: (ctx) => {
toast.error(ctx.error.message);
},
}
)


Reply to this email directly, view it on GitHub
https://github.com/better-auth/better-auth/issues/180#issuecomment-2414509240,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/A5RGP2XYFP5JJ57SKQPM52TZ3VANJAVCNFSM6AAAAABP7ODJLSVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDIMJUGUYDSMRUGA
.
You are receiving this because you authored the thread.Message ID:
@.***>

@firstaxel commented on GitHub (Oct 15, 2024): I tried the tanstack approach but there is a issue associated with that , which is you can't do the same with socials cause it need callbackurl which will not be redirected with tansack query and also with email, there is a issue with when signing up with email it is complicated . the way I will use Bereket way but having to tackle with usestate, I just wish we could have the same time done like tansack query which we have the mutate, isloading, data, error it will be a good developer experience and also for the user On Tue, Oct 15, 2024, 5:39 PM Bereket Engida ***@***.***> wrote: > you can do what @Loa212 <https://github.com/Loa212> recommended here or > you can also have your own loading state and set it on a callback. > > const [loading, setLoading] = useState(false) > await signIn.email( > { > email, > password > }, > { > onResponse: () => { > setLoading(false); > }, > onRequest: () => { > setLoading(true); > }, > onError: (ctx) => { > toast.error(ctx.error.message); > }, > } > ) > > — > Reply to this email directly, view it on GitHub > <https://github.com/better-auth/better-auth/issues/180#issuecomment-2414509240>, > or unsubscribe > <https://github.com/notifications/unsubscribe-auth/A5RGP2XYFP5JJ57SKQPM52TZ3VANJAVCNFSM6AAAAABP7ODJLSVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDIMJUGUYDSMRUGA> > . > You are receiving this because you authored the thread.Message ID: > ***@***.***> >
Author
Owner

@Loa212 commented on GitHub (Oct 15, 2024):

You can do it like this:

const {data, isPending, error} = useMutation({
    mutationFn: async () => {
         return await client.signIn.social({
           provider: "github",
           callbackURL: "/dashboard",
          })
       },
  });
@Loa212 commented on GitHub (Oct 15, 2024): You can do it like this: ```tsx const {data, isPending, error} = useMutation({ mutationFn: async () => { return await client.signIn.social({ provider: "github", callbackURL: "/dashboard", }) }, }); ```
Author
Owner

@Bekacru commented on GitHub (Oct 16, 2024):

I just wish we could have the same time done like tansack query which we have the mutate, isloading, data, error it will be a good developer experience and also for the user

This would require us to turn all the client methods into hooks, which we're not planning to do because we want the client to remain agnostic and consistent across different frameworks.

@Bekacru commented on GitHub (Oct 16, 2024): > I just wish we could have the same time done like tansack query which we have the mutate, isloading, data, error it will be a good developer experience and also for the user This would require us to turn all the client methods into hooks, which we're not planning to do because we want the client to remain agnostic and consistent across different frameworks.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#77