next.js 在Route.ts文件中未导出HTTP方法

qmb5sa22  于 2023-10-18  发布在  其他
关注(0)|答案(1)|浏览(175)

面对错误时,试图实现一个代码,这是原来在Next.js 12与网页路由器使用JavaScript,通过使用Next.js 13与类型脚本。
已在“vibe\src\app\API\auth[. nextauth]\route. ts”中检测到默认导出。而是为每个HTTP方法导出命名导出。
未在“vibe\src\app\API\auth[. nextauth]\route. ts”中导出HTTP方法。为每个HTTP方法导出命名导出。[next-auth][error][CLIENT_FETCH_ERROR] https://next-auth.js.org/errors#client_fetch_error JSON输入意外结束{ error:{
message:'Unexpected end of JSON input',stack:'语法错误:JSON输入\n“+”在JSON处意外结束。parse()\n“+”在packageData(node:internal/deps/undici/undici:6403:23)\n“+”在specConsumeBody(node:internal/deps/undici/undici:6381:14)\n“+
' at process.processTicksAndRejections(node:internal/process/task_queues:95:5)',名称:'语法错误'
},url:'http://localhost:3000/api/auth/providers',
而不是对每个HTTP方法进行测试。
未在“src\app\API\auth[. nextauth]\route. ts”中导出HTTP方法。为每个HTTP方法导出命名导出。
下面是您可以参考的route.ts文件,

import NextAuth from "next-auth";
import SpotifyProvider from "next-auth/providers/spotify";
import spotifyApi from "@/lib/spotify";
import { LOGIN_URL } from "@/lib/spotify";

async function refreshAccessToken(token: any) {

  try {
    spotifyApi.setAccessToken(token.accessToken);
    spotifyApi.setRefreshToken(token.refreshToken);

    const { body: refreshedToken } = await spotifyApi.refreshAccessToken();
    console.log("refreshed token is", refreshedToken);

    return {
      ...token,
      accessToken: refreshedToken.access_token,
      accessTokenExpires: refreshedToken.expires_in * 1000 + Date.now(),
      refreshToken: refreshedToken.refresh_token || token.refreshToken,
    };
    

  } catch (error) {
    console.log(error);

    return {
      ...token,
      error: "RefreshAccessTokenError",
    };
  }
}

const authOptions = {

  providers: [
    SpotifyProvider({
      clientId: `${process.env.NEXT_PUBLIC_CLIENT_ID}`,
      clientSecret: `${process.env.NEXT_PUBLIC_CLIENT_SECRET}`,
      authorization: LOGIN_URL,
    }),
    // ...add more providers here
  ],
  secret: process.env.JWT_SECRET,
  pages: {
    signIn: "/login",
  },
  callback: {
    async jwt({
      token,
      account,
      user,
    }: {
      token: any;
      account: any;
      user: any;
    }) {

      if (account && user) {
        return {
          ...token,
          accessToken: account.accessToken,
          refreshToken: account.refreshToken,
          username: account.providerAccountId,
          accessTokenExpires: account.expires_at + 1000,
        };
      }

      if (Date.now() < token.accessTokenExpires) {
        console.log("token is valid");
        return token;
      }

      console.log("token is invalid");
      return await refreshAccessToken(token);
    },

    async session({ session, token }: { session: any; token: any }) {
      session.user.accessToken = token.accessToken;
      session.user.refreshToken = token.refreshToken;
      session.user.username = token.username;

      return session;
    },
  },
};

export default NextAuth(authOptions);

完整的代码可以参考https://github.com/dipesh2508/Vibe

jv2fixgn

jv2fixgn1#

API Routes在Next.js v13中被Route Handlers取代。
v12:https://nextjs.org/docs/pages/building-your-application/routing/api-routes
v13:https://nextjs.org/docs/app/building-your-application/routing/route-handlers
route.js:https://nextjs.org/docs/app/api-reference/file-conventions/route
你的错误是解释没有HTTP方法从你的路由处理程序中导出。例如GET()
NextAuth文档中的这一页展示了每个版本。
https://next-auth.js.org/configuration/initialization#route-handlers-app
虽然注意到新的文档是在这个域:https://authjs.dev/
希望这对你有帮助!

相关问题