Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error =TypeError: "ikm"" must be an instance of Uint8Array or a string

I use next.js typescript,next-auth,prisma,mangodb and I got this error:
TypeError: "ikm"" must be an instance of Uint8Array or a string in vscode
And I got these 2 errors in console:
Error 1 Failed to load resource: the server responded with a status of 500 (Internal Server Error)
Error 2 SyntaxError: Unexpected token '<', "<!DOCTYPE "... is not valid JSON

this is my [...nextauth]:

import NextAuth from 'next-auth';
import GithubProvider from 'next-auth/providers/github';
import GoogleProvider from 'next-auth/providers/google';
import Credentials from 'next-auth/providers/credentials';
import { PrismaAdapter } from '@next-auth/prisma-adapter';
import { compare } from 'bcrypt';
import prismadb from '../../../lib/prismadb';

export default NextAuth({
  providers: [
    GithubProvider({
      clientId: process.env.GITHUB_ID || '',
      clientSecret: process.env.GITHUB_SECRET || '',
    }),
    GoogleProvider({
      clientId: process.env.GOOGLE_CLIENT_ID || '',
      clientSecret: process.env.GOOGLE_CLIENT_SECRET || '',
    }),
    Credentials({
      id: 'credentials',
      name: 'Credentials',
      credentials: {
        email: {
          label: 'Email',
          type: 'text',
        },
        password: {
          label: 'Password',
          type: 'passord',
        },
      },
      async authorize(credentials) {
        if (!credentials?.email || !credentials?.password) {
          throw new Error('Email and password required');
        }

        const user = await prismadb.user.findUnique({
          where: {
            email: credentials.email,
          },
        });

        if (!user || !user.hashedPassword) {
          throw new Error('Email does not exist');
        }

        const isCorrectPassword = await compare(
          credentials.password,
          user.hashedPassword
        );

        if (!isCorrectPassword) {
          throw new Error('Incorrect password');
        }

        return user;
      },
    }),
  ],
  pages: {
    signIn: '/auth',
  },
  debug: process.env.NODE_ENV === 'development',
  adapter: PrismaAdapter(prismadb),
  session: { strategy: 'jwt' },
  jwt: {
    secret: process.env.NEXTAUTH_JWT_SECRET,
  },
  secret: process.env.NEXTAUTH_SECRET,
});

and this one is my prismadb:

import { PrismaClient } from '@prisma/client';

const client = global.prismadb || new PrismaClient();
if (process.env.NODE_ENV === 'production') global.prismadb = client;
export default client;

and this one is global.d.ts:

import { PrismaClient } from '@prisma/client';
import type { MongoClient } from 'mongodb';
declare global {
  namespace globalThis {
    var prismadb: PrismaClient | undefined;
  }
}

The thing is when I try to register I got error but account I create in database.

like image 412
arshamghobadi Avatar asked May 23 '26 13:05

arshamghobadi


1 Answers

I had same exact error, i was able to register but not login.

Reason is typo!

Correct configuration

NEXTAUTH_JWT_SECRET="xxxxx"

NEXTAUTH_SECRET="yyyyy"

Wrong configuration

NEXAUTH_JWT_SECRET="xxxxx"

NEXTAUTH_SECRET="yyyyy"

T was missing => Before : NEXAUTH_JWT_SECRET After : NEXTAUTH_JWT_SECRET

like image 166
shareef Avatar answered May 26 '26 06:05

shareef



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!