Step 1
Let's add a new file, User.js
to the model
folder with the following content:
const mongoose = require("mongoose");
const UserSchema = new mongoose.Schema({
username: { type: String, required: true, unique: true },
password: { type: String, required: true },
role: {
type: String,
enum: ["CLIENT", "ADMIN"],
required: true,
},
});
const User = mongoose.model("User", UserSchema);
module.exports = User;
Notice we represent a user in its simplest form, with a username, a password, and a role! You often need more information to define a user in your application.
Note how the role
attribute is an enum. For simplicity, we assume users are either "clients" or "admins." Moreover, we expect each user has a unique username.