0

I am implementing NextAuth with my NextJS(GraphQL) app. I have this error:

Unhandled Runtime Error
TypeError: Cannot use 'in' operator to search for 'credentials' in null

The network error also shows:

Error: HTTP GET is not supported for /api/auth/session

Error: HTTP GET is not supported for /api/auth/providers

I am not making a GET request anywhere I know. My code is below:

pages/api/[...nextauth.js]


const providers = [
  Providers.Credentials({
    name: 'Credentials',
    authorize: async (credentials) => {
      try {
        const user = await <GraphQL Logic to fetch user>

        if (user) {
          return {status: 'success', data: user.data.user}
        } 
      } catch (e) {
        const errorMessage = e.response.data.message
        // Redirecting to the login page with error messsage in the URL
        throw new Error(errorMessage + '&email=' + credentials.email)
      }

    }
  })
]

const callbacks = {
  async jwt(token, user) {
    if (user) {
      token.accessToken = user.data.token
    }

    return token
  },

  async session(session, token) {
    session.accessToken = token.accessToken
    return session
  }
}

const options = {
  providers,
  callbacks,
  pages: {
    error: '/login' // Changing the error redirect page to our custom login page
  }
}

export default (req, res) => NextAuth(req, res, options)

pages/login.js

<form onSubmit={(e) => handleLogin(e)}>
            <input value={email} onChange={(e) => setEmail(e.target.value)} />
            <input type='password' value={password} onChange={(e) => setPassword(e.target.value)} />
            <button type='submit' disabled={isLoginStarted}>Log In</button>
          </form>

handleLogin method

const handleLogin = (e) => {
    e.preventDefault()
    setIsLoginStarted(true)
    signIn('credentials',
      {
        email,
        password,
        callbackUrl: `${window.location.origin}/welcome`
      }
    )
  }
2
  • Could you solve the problem? Commented May 26, 2021 at 12:44
  • I have same issue. Commented May 26, 2021 at 12:45

2 Answers 2

1

Why the URL look like that

pages/api/[...nextauth.js]

it must be

pages/api/[...nextauth].js

or if you are using next.js 13.4, then

pages/api/[...nextauth]/route.js
Sign up to request clarification or add additional context in comments.

Comments

0

Signin method takes in a provider id as its first argument. Therefore, all methods are called for a specific provider. In your case you are calling a signin method for provider credentials but in your configuration, there is no provider with id credentials.

To fix this, set id: 'credentials' in your credentials provider config:

const providers = [
  Providers.Credentials({
    id: 'credentials',
    name: 'Credentials',
    authorize: async (credentials) => {
      try {
        const user = await <GraphQL Logic to fetch user>

        if (user) {
          return {status: 'success', data: user.data.user}
        } 
      } catch (e) {
        const errorMessage = e.response.data.message
        // Redirecting to the login page with error messsage in the URL
        throw new Error(errorMessage + '&email=' + credentials.email)
      }

    }
  })
]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.