Firing useEffect after signIn with nextAuth
Unanswered
Cape lion posted this in #help-forum
Cape lionOP
is tehre any way to know when sign in just happened on the client side?
22 Replies
@Cape lion is tehre any way to know when sign in just happened on the client side?
there should be a return promise from when you call the signIn() from next-auth/react. Can you check on that?
Cape lionOP
ooh interesting
lets see
Cape lionOP
Ok it didn't work
I made a react hook for it
import { useState } from "react";
import { signIn, useSession } from "next-auth/react";
import type {
LiteralUnion,
SignInAuthorizationParams,
SignInOptions,
} from "next-auth/react";
import { useAnalytics } from "use-analytics";
import type { BuiltInProviderType } from "next-auth/providers";
export function useTrackedSignIn() {
const [isSigningIn, setIsSigningIn] = useState(false);
const { track } = useAnalytics();
const { data: session, status } = useSession();
const trackedSignIn = async (
provider?: LiteralUnion<BuiltInProviderType, string>,
options?: SignInOptions,
authorizationParams?: SignInAuthorizationParams
) => {
setIsSigningIn(true);
const result = await signIn(provider, options, authorizationParams);
console.log(
`########## ##########
######### ### result
###################`,
result
);
debugger;
if (!result) {
debugger;
track("user-signin-error", {
error: "undefined-error",
});
setIsSigningIn(false);
return {
error: "An error occurred while signing in",
};
}
const { error, url } = result;
if (error) {
track("user-signin-error", {
error: error,
provider: provider,
method: options?.method,
});
} else {
track("user-signin-success", {
provider: provider,
status: "success",
ok: true,
url: url,
method: options?.method,
});
}
setIsSigningIn(false);
return result;
};
return {
isSigningIn,
session,
status,
signIn: trackedSignIn,
};
}the user-signin-error - undefined error is always returned
it's returned even before the signIn is complete
but i guess what i should do is just append a query string to the callback url
and capture that
signIn="true"
something like that
then capture that in _app
the key point here is that this promise is only used when you want disable the default redirection
or mabye...
ok what if i set redirect to false
and then redirect inside my hook
doesnt work for google
ok lets see
what if i append that query string
Cape lionOP
there's gotta be a better way to do this