Spaces:
Running
Running
| import mongoose from 'mongoose'; | |
| const deviceSchema = new mongoose.Schema( | |
| { | |
| deviceId: { type: String, required: true }, | |
| label: { type: String, default: 'Unknown Device' }, | |
| lastSeen: { type: Date, default: Date.now }, | |
| }, | |
| { _id: false }, | |
| ); | |
| const UserSchema = new mongoose.Schema( | |
| { | |
| name: { type: String, required: true }, | |
| email: { type: String, required: true, unique: true }, | |
| password: { type: String }, // optional: Google/OAuth users have no password | |
| role: { type: String, enum: ['farmer', 'expert', 'admin'], default: 'farmer' }, | |
| img: { type: String }, | |
| country: { type: String }, | |
| phone: { type: String }, | |
| desc: { type: String }, | |
| isSeller: { type: Boolean, default: false }, | |
| // ββ Firebase / Google OAuth link βββββββββββββββββββββββββββββββββββββ | |
| // Stored so that register-device and other routes can look up the user | |
| // by Firebase UID instead of MongoDB _id when using Google sign-in. | |
| firebaseUid: { type: String, sparse: true, index: true }, | |
| language: { type: String, default: 'en' }, | |
| // ββ Cross-device login tracking ββββββββββββββββββββββββββββββββββββββ | |
| devices: { type: [deviceSchema], default: [] }, | |
| }, | |
| { timestamps: true }, | |
| ); | |
| export default mongoose.models.User || mongoose.model('User', UserSchema); | |