Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/Priyanshu471/ad-management/llms.txt

Use this file to discover all available pages before exploring further.

The User model represents a registered account in the Ad Management System. Defined in models/user.ts, it uses a Mongoose schema to persist user credentials and role assignments to MongoDB. Every campaign belongs to a user, making this model the root of all ownership relationships in the platform.

Schema

models/user.ts
import mongoose, { Schema, models } from "mongoose";

const userSchema = new Schema(
  {
    name:     { type: String, required: true },
    email:    { type: String, unique: true, required: true },
    password: { type: String, required: true },
    role:     { type: String, required: true },
  },
  { timestamps: true }
);

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

Fields

_id
ObjectId
Auto-generated unique identifier assigned by MongoDB on document creation.
name
String
required
The user’s display name shown across the dashboard.
email
String
required
The user’s email address. Must be unique across all accounts and is used as the login identifier.
password
String
required
The user’s password. See the security warning below regarding the current storage implementation.
role
String
required
Determines which dashboard the user is directed to after login. The schema accepts any string — the conventional application values are admin, advertiser, and creator.
createdAt
Date
Timestamp automatically set by Mongoose when the document is first created.
updatedAt
Date
Timestamp automatically updated by Mongoose whenever the document is modified.

Sample Document

{
  "_id": "65f1a2b3c4d5e6f7a8b9c0d1",
  "name": "jane doe",
  "email": "jane@example.com",
  "password": "secret123",
  "role": "advertiser",
  "createdAt": "2024-01-15T10:30:00.000Z",
  "updatedAt": "2024-01-15T10:30:00.000Z"
}

Role Values

The role field controls routing after a successful login. The schema stores any string — no enum validation is applied at the database layer. The conventional values used by the application are:
ValueDashboard
admin/admin
advertiser/advertiser
creator/creator

Model Registration Pattern

The model is exported using the guard pattern:
const User = models.User || mongoose.model("User", userSchema);
This prevents Mongoose from throwing the "Cannot overwrite model once compiled" error in Next.js hot-reload environments. During development, Next.js re-executes module code on every hot reload. Without this guard, each reload would attempt to recompile the User model against a connection that already has it registered. The models.User check short-circuits that path and returns the already-compiled model instead.

Security

Passwords are currently stored as plain text in the database. The bcryptjs package is already listed as a project dependency — hashing should be integrated before this application is used in any non-development environment.Before saving a new user, hash the password:
import bcrypt from "bcryptjs";

const hashedPassword = await bcrypt.hash(password, 10);
// store hashedPassword instead of the raw password
On login, compare the input against the stored hash:
const isValid = await bcrypt.compare(inputPassword, storedHashedPassword);

Build docs developers (and LLMs) love