最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

next auth - NextAuth; Trouble importing provider - Stack Overflow

programmeradmin3浏览0评论

I'm using NextAuth with Spotify authentication, but I'm running into an import issue with SpotifyProvider. When I try to run my app, I get the following error:

Error: Module not found: Can't resolve 'next-auth/providers/spotify'

I've tried reinstalling next-auth (npm install next-auth), clearing node_modules, and changing the import to import Spotify from "next-auth/providers/spotify", but nothing seems to work.

What am I doing wrong?

    import NextAuth from "next-auth";
    import SpotifyProvider from "next-auth/providers/spotify";

    async function refreshAccessToken(token) {
      try {
        const response = await fetch(";, {
          method: "POST",
          headers: {
            "Content-Type": "application/x-www-form-urlencoded",
            Authorization: `Basic ${Buffer.from(
              `${process.env.SPOTIFY_CLIENT_ID}:${process.env.SPOTIFY_CLIENT_SECRET}`
            ).toString("base64")}`,
          },
          body: new URLSearchParams({
            grant_type: "refresh_token",
            refresh_token: token.refreshToken,
          }),
        });

        const refreshedTokens = await response.json();

        if (!response.ok) {
          throw refreshedTokens;
        }

        return {
          ...token,
          accessToken: refreshedTokens.access_token,
          accessTokenExpires: Date.now() + refreshedTokens.expires_in * 1000, // 1 hour
          refreshToken: refreshedTokens.refresh_token ?? token.refreshToken, // Some APIs may not return a new refresh token
        };
      } catch (error) {
        console.error("Error refreshing access token", error);
        return { ...token, error: "RefreshAccessTokenError" };
      }
    }

    export default NextAuth({
      providers: [
        SpotifyProvider({
          clientId: process.env.SPOTIFY_CLIENT_ID,
          clientSecret: process.env.SPOTIFY_CLIENT_SECRET,
          authorization:
            " user-read-email",
        }),
      ],
      secret: process.env.NEXTAUTH_SECRET,
      callbacks: {
        async jwt({ token, account }) {
          if (account) {
            return {
              accessToken: account.access_token,
              accessTokenExpires: Date.now() + account.expires_in * 1000, // 1 hour
              refreshToken: account.refresh_token,
            };
          }

          if (Date.now() > token.accessTokenExpires) {
            return await refreshAccessToken(token);
          }

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

Here is my package.json which is in the root directory:

    {
      "dependencies": {
        "dotenv": "^16.4.7",
        "mongoose": "^8.12.2",
        "next": "^13.2.0",
        "next-auth": "^4.24.11",
        "react": "^18.2.0",
        "react-dom": "^18.2.0"
      },
      "name": "music-player-backend",
      "version": "1.0.0",
      "main": "app.js",
      "type": "module",
      "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1",
        "dev": "next dev",
        "build": "next build",
        "start": "next start"
      },
      "keywords": [],
      "author": "",
      "license": "ISC",
      "description": ""
    }

发布评论

评论列表(0)

  1. 暂无评论