Next.js Discord

Discord Forum

NextAuth route handler (eslint and "any" type error)

Answered
Brewer's Blackbird posted this in #help-forum
Open in Discord
Brewer's BlackbirdOP
I'm trying to add NextAuth to my project with app router and in "app/api/auth/[...nextauth]/route.ts" I added the following code:

import NextAuth from "next-auth/next";
import { authOptions } from "~/lib/auth";

const handler = NextAuth(authOptions); (error: Unsafe assignment of an `any` value.)

export { handler as GET, handler as POST };


"src/lib/auth":

[...]

export const authOptions: NextAuthOptions = {
  callbacks: {
    session: ({ session, user }) => ({
      ...session,
      user: {
        ...session.user,
        id: user.id,
      },
    }),
  },
  adapter: PrismaAdapter(prisma),
  providers: [
    DiscordProvider({
      clientId: env.DISCORD_CLIENT_ID,
      clientSecret: env.DISCORD_CLIENT_SECRET,
    }),
  ],
};

[...]


".eslintrc.cjs":

// eslint-disable-next-line @typescript-eslint/no-var-requires
const path = require("path");

/** @type {import("eslint").Linter.Config} */
const config = {
  overrides: [
    {
      extends: [
        "plugin:@typescript-eslint/recommended-requiring-type-checking",
      ],
      files: ["*.ts", "*.tsx"],
      parserOptions: {
        project: path.join(__dirname, "tsconfig.json"),
      },
    },
  ],
  parser: "@typescript-eslint/parser",
  parserOptions: {
    project: path.join(__dirname, "tsconfig.json"),
  },
  plugins: ["@typescript-eslint"],
  extends: ["next/core-web-vitals", "plugin:@typescript-eslint/recommended"],
  rules: {
    "@typescript-eslint/consistent-type-imports": [
      "warn",
      {
        prefer: "type-imports",
        fixStyle: "inline-type-imports",
      },
    ],
    "@typescript-eslint/no-unused-vars": ["warn", { argsIgnorePattern: "^_" }],
  },
};

module.exports = config;
Answered by Brewer's Blackbird
I solver it:
import { type NextApiHandler } from "next";
import NextAuth from "next-auth/next";

import { authOptions } from "~/lib/auth";

const handler: NextApiHandler = (req, res) => {
  return NextAuth(req, res, authOptions);
};

export { handler as GET, handler as POST };
View full answer

2 Replies

Brewer's BlackbirdOP
"tsconfig.json":

{
  "compilerOptions": {
    "target": "es2017",
    "lib": ["dom", "dom.iterable", "esnext"],
    "allowJs": true,
    "checkJs": true,
    "skipLibCheck": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noEmit": true,
    "esModuleInterop": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "preserve",
    "incremental": true,
    "noUncheckedIndexedAccess": true,
    "baseUrl": ".",
    "paths": {
      "~/*": ["./src/*"]
    },
    "plugins": [
      {
        "name": "next"
      }
    ]
  },
  "include": [
    ".eslintrc.cjs",
    "next-env.d.ts",
    "**/*.ts",
    "**/*.tsx",
    "**/*.cjs",
    "**/*.mjs",
    ".next/types/**/*.ts"
  ],
  "exclude": ["node_modules"]
}
Brewer's BlackbirdOP
I solver it:
import { type NextApiHandler } from "next";
import NextAuth from "next-auth/next";

import { authOptions } from "~/lib/auth";

const handler: NextApiHandler = (req, res) => {
  return NextAuth(req, res, authOptions);
};

export { handler as GET, handler as POST };
Answer