Need help to authenticate the user using Next-auth and MongoDB
Unanswered
Santhosh Prabhakaran posted this in #help-forum
I've created my back end DB using express and MongoDB as an separate server folder and the next app in another client folder.
This is my app/api/auth/[...nextauth]/route.js
const handler = NextAuth(authOptions);
export { handler as GET, handler as POST };
it is working fine for now. But I need to authenticate the user cred here with the data in mongoose.
The mongoose document is created in the name of "Users" model.
How to approach it since the model is not created within the Next app outside instead ?
This is my app/api/auth/[...nextauth]/route.js
import NextAuth from "next-auth/next";
import CredentialsProvider from "next-auth/providers/credentials";
export const authOptions = {
providers: [
CredentialsProvider({
name: "Credentials",
credentials: {
username: { label: "Username", type: "text", placeholder: "Username" },
password: { label: "Password", type: "text", placeholder: "Password" },
},
async authorize(credentials, req) {
if (credentials.username == "admin" && credentials.password == "123")
return { id: 1, name: "Santhosh", email: "santhosh@mail.com" };
return null;
},
}),
],
};const handler = NextAuth(authOptions);
export { handler as GET, handler as POST };
it is working fine for now. But I need to authenticate the user cred here with the data in mongoose.
The mongoose document is created in the name of "Users" model.
import mongoose from "mongoose";
const UserSchema = new mongoose.Schema(
{
username: { type: String, required: true },
password: { type: String, required: true }
},
{ timestamps: true }
);
export const User = mongoose.models.User || mongoose.model("User", UserSchema);How to approach it since the model is not created within the Next app outside instead ?