Help Needed: ReferenceError: Cannot Access 'authConfig' Before Initialization in NextAuth Setup #11276
Unanswered
shkarDiary
asked this question in
Help
Replies: 2 comments 2 replies
-
Also getting the same error, am on next-auth version 5.0.0-beta.19. I have two files, one for the config and the other for the actual auth: import Credentials from "next-auth/providers/credentials";
import type { NextAuthConfig } from "next-auth";
import { loginSchema } from "@/schemas/auth-schema";
import { getUserByEmail } from "@/queries/auth-queries";
import { scryptSync } from "crypto";
export const authConfig = {
providers: [
Credentials({
async authorize({ credentials }) {
const validatedData = loginSchema.safeParse(credentials);
if (!validatedData.success) return null;
const { email, password } = validatedData.data;
const user = await getUserByEmail(email);
if (!user || !user.password) return null;
const salt = user.password.slice(64);
const originalPassHash = user.password.slice(0, 64);
const currentPassHash = scryptSync(password, salt, 32).toString("hex");
const passIsValid = originalPassHash === currentPassHash;
if (!passIsValid) return null;
return user;
},
}),
],
} satisfies NextAuthConfig;
import NextAuth from "next-auth";
import { authConfig } from "./auth.config";
import { PrismaAdapter } from "@auth/prisma-adapter";
import { db } from "@/lib/db";
export const { auth, handlers, signIn, signOut } = NextAuth({
adapter: PrismaAdapter(db),
session: { strategy: "jwt" },
...authConfig,
}); My api route: import { handlers } from "@/auth";
export const { GET, POST } = handlers; |
Beta Was this translation helpful? Give feedback.
1 reply
-
So I found a fix, apparently, for whatever reason, making the query to get the user inside the const user = await getUserByEmail(email); Just making the query directly: const user = await db.user.findUnique({
where: {
email: email as string,
},
}); fixes it. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello everyone,
I've been working on setting up authentication in my Next.js application using NextAuth, and I've encountered a persistent issue that I hope to get some expert advice on.
Issue
I'm receiving the following error when trying to initialize my NextAuth configuration:
Here's the relevant code snippet and additional context:
auth.config.ts
auth.ts
I'm seeking guidance on the following:
Potential Causes: What could be causing this ReferenceError?
Best Practices: Are there any best practices for organizing and initializing NextAuth configurations to avoid such issues?
Troubleshooting Tips: Any additional debugging steps I should consider?
Beta Was this translation helpful? Give feedback.
All reactions