Skip to content

How to trigger FormMessage to display an error when failed to login in NextAuth #2087

Answered by onurhan1337
kemaltf asked this question in Q&A
Discussion options

You must be logged in to vote

In your code, you're using setError from react-hook-form to manually set an error message when the sign-in fails. However, you're not using this error state to display the error message in your form.

To display the error message, you need to pass the error state to the FormMessage component. Here's how you can do it:

<FormField
  control={form.control}
  name="password"
  render={({ field }) => (
    <FormItem>
      <FormLabel>Password</FormLabel>
      <FormControl>
        <Input type="password" placeholder="Password" {...field} />
      </FormControl>
      {errors.password && <FormMessage>{errors.password.message}</FormMessage>}
    </FormItem>
  )}
/>

In this code, errors.password w…

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
1 reply
@kemaltf
Comment options

Answer selected by kemaltf
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants