Next.js Discord

Discord Forum

Issue with auth.js when creating user with QAuth profile into mongodb with mongoose

Unanswered
American posted this in #help-forum
Open in Discord
Avatar
AmericanOP
Whenever I am importing User model in auth.js the page freezes.
When I use Mongodb adaptor through crypto error.

User Model:
import mongoose from "mongoose";

const userSchema = new mongoose.Schema({
name: { type: String, required: true },
stateId: { type: mongoose.ObjectId, ref: "State" },
districtId: { type: mongoose.ObjectId, ref: "District" },
constituencyId: { type: mongoose.ObjectId, ref: "Constituency" },
cityId: { type: mongoose.ObjectId, ref: "City" },
ward: { type: Number },
});

const User = mongoose.models.User || mongoose.model("User", userSchema);

export default User;

Auth.js
const providers = [Google,
Facebook,
Twitter,
Apple,],
export const providerMap = providers
.map((provider) => {
if (typeof provider === "function") {
const providerData = provider();
return { id: providerData.id, name: providerData.name };
} else {
return { id: provider.id, name: provider.name };
}
})
.filter((provider) => provider.id !== "credentials");

export const { handlers, signIn, signOut, auth } = NextAuth({
adapter: MongoDBAdapter(connectToDatabase),
// providers: [GitHub, Google],
providers,
pages: {
signIn: "/login",
},
callbacks: {
signIn: async (params) => {
try {
await connectToDatabase();
await User.create({
name: params.profile.name,
stateId: "673b4bcea7b8e2a0f8ed963f",
districtId: "673b4ea6a7b8e2a0f8ed9643",
constituencyId: "673cb392160677b1437aa534",
cityId: "673c54ad160677b1437aa527",
ward: 50,
});
} catch (error) {
console.log("Login error", error);
}

0 Replies