【问题标题】:Stuck with Next Auth authorize part. Error: Action api with HTTP GET is not supported by NextAuth.js卡在 Next Auth 授权部分。错误:NextAuth.js 不支持带有 HTTP GET 的操作 api
【发布时间】:2022-03-11 07:25:45
【问题描述】:

过去几天我遇到了一个奇怪的错误。无法使用凭据对我的 Next.js 应用程序进行身份验证。

据我所知,根据文档,一切都是正确的。

我也无法在我的 chrome 开发工具中看到网络请求。

import NextAuth from "next-auth";
import CredentialProvider from "next-auth/providers/credentials";
import { signOut,useSession,signIn } from 'next-auth/react';
import { axios } from 'axios';
import { API_URL } from "../../../helpers/api/mutations";

export default NextAuth({
  providers: [
    CredentialProvider({
      name: "credentials",
      credentials: {
        username: {
          label: "Email",
          type: "text",
          placeholder: "mani@test.com"
        },
        password: {
          label: "Password",
          type: "password"
        }},
        // My authorize function where I called my graphql mutation for token and session.
        async authorize(credentials,req) {
            const {data} = await axios({
              url: API_URL,
              method: 'post',
              data: {
               query: `mutation {
                login(
                   mobileNumber: "4738291046",
                  mobileCountryCode: "00",
                  password: "admin123"
                ) {
                  expiresAt
                  accessToken
                }
              }`
              }
             })
              .then(res => {
               console.log(res.data);
              })
              .catch(err => {
               console.log(err.message);
              });

              if (data) {
                console.log(data,'dataaa');
                return data
              }
              else {
                return null
              }
        }
    }),
  ],
  callbacks: {
    jwt: ({ token, user }) => {
      // first time jwt callback is run, user object is available
      if (user) {
        token.id = user.id;
      }

      return token;
    },
    session: ({ session, token }) => {
      if (token) {
        session.id = token.id;
      }

      return session;
    },
  },
   secret: "test",
   jwt: {
       secret:"test",
       encryption: true,
   },
   pages: {
    signIn: "api/auth/sigin",
  },
});

【问题讨论】:

    标签: javascript node.js reactjs next.js next-auth


    【解决方案1】:

    我遇到了这个问题,因为我忘记了自定义登录页面值中的前导 /

    pages: {
      signIn: "/api/auth/sigin",
    }
    

    【讨论】:

      【解决方案2】:

      问题可能与您在authorize 函数中如何处理async/await 有关。

      尝试像这样改变它:

      async authorize(credentials,req) {
          try {
              const { data } = await axios({
                  url: API_URL,
                  method: 'post',
                  data: {
                      query: `mutation {
                          login(
                          mobileNumber: "4738291046",
                          mobileCountryCode: "00",
                          password: "admin123"
                          ) {
                          expiresAt
                          accessToken
                          }
                      }`
                  }
              })
      
              if (data) {
                  console.log(data,'dataaa');
                  return data
              }
              else {
                  return null
              }
          } catch (err) {
              return null
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2017-06-02
        • 2010-10-03
        • 1970-01-01
        • 2022-11-02
        • 1970-01-01
        • 1970-01-01
        • 2019-11-01
        • 2022-10-25
        • 1970-01-01
        相关资源
        最近更新 更多