You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If you return null then an error will be displayed advising the user to check their details.
Using the credentials provider to sign in causes the page to refresh and an invalid login doesn't allow me to catch and handle any built in errors if it is returning null.
Form/hook use
constonSubmit=form.handleSubmit(async(data,event)=>{event?.preventDefault();awaitsignIn("credentials",{ ...data}).then(()=>router.push("/")).catch(()=>{form.setError("root",{message: "Sign in failed. Please try again.",});});});
Backend Logic
constgetUserByEmail=(email: string)=>db.select().from(schema.user).where(eq(schema.user.email,email)).then(takeFirstOrNull);exportconstgetUserByCredentials=async(email: string,password: string)=>{constuser=awaitgetUserByEmail(email);if(user==null)returnnull;const{ passwordHash }=user;if(passwordHash==null)returnnull;constisPasswordCorrect=compareSync(password,passwordHash);returnisPasswordCorrect ? user : null;};
Provider Logic
constproviders=(): Provider[]=>{constp: Provider[]=[];
...
if(isCredentialsAuthEnabled)p.push(Credentials({credentials: {email: {},password: {}},authorize: async(credentials)=>{try{const{ email, password }=signInSchema.parse(credentials);constuser=awaitgetUserByCredentials(email,password);console.log(user);returnuser;}catch(error){console.log(error);// Return `null` to indicate that the credentials are invalidif(errorinstanceofZodError)returnnull;throwerror;}},}),);returnp;};
How to reproduce
Create a from that uses signIn and a credentials provider that returns null when a credential isn't found.
Triggering that submit hook will cause the page to refresh instead of signIn throwing an error that can gracefully be handled.
Expected behavior
I expected the signIn to throw an error if it receives a null response from the provider. It isn't possible to log in if the credential is null and while I can work around this by changing the logic to the following, it leaves more room for someone to accidentally leak if a user exists, and forces errors to be thrown and caught that don't create a lot of value.
Hi there,
What I understand by your issue is that you want to throw some error when the authentication fails. We had a long discussion with a solution here #11747 (comment) Check this out I have given a full example with images and two different ways to get the error on the client. If still you have issues you can ask. Peace
You can do this in a server action or so and it will throw the error Also note that if you have to handle separately when using the server signIn() and client signIn() from next-auth/react
Provider type
Credentials
Environment
Reproduction URL
https://github.com/ctrlplanedev/ctrlplane
Describe the issue
In the documentation it states
Using the credentials provider to sign in causes the page to refresh and an invalid login doesn't allow me to catch and handle any built in errors if it is returning
null
.Form/hook use
Backend Logic
Provider Logic
How to reproduce
Create a from that uses
signIn
and acredentials
provider that returns null when a credential isn't found.Triggering that submit hook will cause the page to refresh instead of
signIn
throwing an error that can gracefully be handled.Expected behavior
I expected the
signIn
to throw an error if it receives anull
response from the provider. It isn't possible to log in if the credential isnull
and while I can work around this by changing the logic to the following, it leaves more room for someone to accidentally leak if a user exists, and forces errors to be thrown and caught that don't create a lot of value.The text was updated successfully, but these errors were encountered: