Custom sign in page not redirecting correctly in next auth

Viewed 4115

api/[...nextauth].js

export default NextAuth({
providers: [
    Providers.Credentials({
    name: 'Credentials',
    credentials: {
        email: { label: 'E-mail', type: 'text', placeholder: 'me@gmail.com' },
        password: { label: 'Password', type: 'password', placeholder: 'my password' },
    },
    async authorize(credentials, req) {
        return { email: 'x', password: 'y'}
    },
    }),
]
});

pages/auth/signin.tsx

export default function SignIn() {
const [password, setPassword] = useState(null)
const [email, setEmail] = useState(null)

const onSubmit = () => {
    signIn("Credentials", { email, password })
}

return (
    <div>
    <label>
        Email
        <input name="email" type="text" onChange={e => setEmail(e.target.value)} />
    </label>
    <label>
        Password
        <input name="password" type="password"  onChange={e => setPassword(e.target.value)} />
    </label>
    <button type="submit" onClick={onSubmit}>Sign in!</button>
    </div>
)
}

onSubmit redirects to the default sign in page /api/auth/signin?callbackUrl=http%3A%2F%2Flocalhost%3A3000%2Fauth%2Fsignin, instead of correctly signin in and redirecting to /. How to fix this?

I am experiencing the same issue also with the custom sign form using form tag (https://next-auth.js.org/configuration/pages#credentials-sign-in)

2 Answers

I found the solution, but I think the documentation is misleading.

What I did is add an id property to my [...nextauth].js provider :

import Credentials from 'next-auth/providers/credentials';

export default NextAuth({
  providers: [
    Credentials({
      id: 'credentials',
      name: 'Credentials',
      credentials: {
        username: { label: 'Adresse email', type: 'email' },
        password: { label: 'Mot de passe', type: 'password' },
      },
      async authorize(credentials, req) {
        console.log(credentials);
        const user = { id: 1, name: 'Jean Dupont', email: 'jean.dupont@yopmail.com' };

        if (user.id === 1) {
        // Any object returned will be saved in `user` property of the JWT
          return user;
        }
        // If you return null or false then the credentials will be rejected
        return null;
        // You can also Reject this callback with an Error or with a URL:
        // throw new Error('error message') // Redirect to error page
        // throw '/path/to/redirect'        // Redirect to a URL
      },
    }),
  ],
  pages: {
    signIn: '/auth/login',
  },
});

I then called the signIn function as before, replacing the name argument with the id and route to '/' as initially desired

// ...
await signIn('credentials', { email, password, callbackUrl: `${window.location.origin}/` });
// ...

The problem is that the documentation does not mention the use of the id attribute in the provider. It is only mentioned in the multiple providers example code : https://next-auth.js.org/providers/credentials#multiple-providers and there is also nothing in the credentials provider configuration page : https://next-auth.js.org/configuration/providers/credentials-provider.

I experienced the same issue, try changing the signIn call to:

signIn("Credentials", {redirect: false, email, password})

Related