0

我正在使用我的 NextJS(GraphQL) 应用程序实现 NextAuth。我有这个错误:

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

网络错误还显示:

错误:/api/auth/session 不支持 HTTP GET

错误:/api/auth/providers 不支持 HTTP GET

我不会GET在我知道的任何地方提出请求。我的代码如下:

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`
      }
    )
  }
4

0 回答 0