Replies: 1 comment
-
After reading the docs again and again, the solution was simply to put the auth logic to a separate function/file. auth logicexport async function authUser(email: string, password: string) {
await connect();
const user: any = await User.findOne({ email: email })
if (user) {
const isPasswordMatch = await bcryptjs.compare(password, user.password);
if (isPasswordMatch) {
return {
id: user._id,
name: user.name,
email: user.email,
credits: user.credits
};
}
} else {
throw new Error("User not found.")
}
return null;
} authorize()async authorize(credentials: any) {
try {
return await authUser(credentials.email, credentials.password); // Return null if user is not found or password doesn't match
} catch (err: any) {
throw new Error(err.message);
}
}, EDIT: |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi, I'm building a NextJS14 application using AuthJs V5 (beta) for authentication and MongoDB for the database. After following the setup process from the documentation, I'm having trouble with the
authorize
function of the Credentials provider.When adding simple logic for the
authorize
function, I constantly get the errorCannot read properties of undefined (reading 'User')
.Exact Error:
Here is my models schema:
Here is the db connection :
Here is the
auth.ts
file:When debugging, I found out that this code segment in the authorize function is causing the error:
However, when querying the database using the schema in other parts of the application, no error occurs.
Beta Was this translation helpful? Give feedback.
All reactions