Some checks failed
🚀 Continuous Integration / 🔧 Backend Tests (18.x) (push) Has been cancelled
🚀 Continuous Integration / 🔧 Backend Tests (20.x) (push) Has been cancelled
🚀 Continuous Integration / 🎨 Frontend Tests (18.x) (push) Has been cancelled
🚀 Continuous Integration / 🎨 Frontend Tests (20.x) (push) Has been cancelled
🚀 Continuous Integration / 🔍 Code Quality (push) Has been cancelled
🚀 Continuous Integration / 🔒 Security Checks (push) Has been cancelled
🚀 Continuous Integration / 🎨 Theme Tests (push) Has been cancelled
🚀 Continuous Integration / ♿ Accessibility Tests (push) Has been cancelled
🚀 Continuous Integration / 📱 Cross-Browser Tests (push) Has been cancelled
🚀 Continuous Integration / 🏗️ Build Tests (push) Has been cancelled
🚀 Continuous Integration / 📊 Performance Tests (push) Has been cancelled
🚀 Continuous Integration / 🎯 Integration Tests (push) Has been cancelled
🚀 Continuous Integration / ✅ All Tests Passed (push) Has been cancelled
97 lines
1.9 KiB
JavaScript
97 lines
1.9 KiB
JavaScript
const mongoose = require('mongoose');
|
|
const bcrypt = require('bcryptjs');
|
|
|
|
// Schema für Eltern/Familie
|
|
const userSchema = new mongoose.Schema({
|
|
email: {
|
|
type: String,
|
|
required: true,
|
|
unique: true,
|
|
lowercase: true,
|
|
trim: true
|
|
},
|
|
password: {
|
|
type: String,
|
|
required: true,
|
|
minlength: 6
|
|
},
|
|
familyName: {
|
|
type: String,
|
|
required: true,
|
|
trim: true
|
|
},
|
|
parentName: {
|
|
type: String,
|
|
required: true,
|
|
trim: true
|
|
},
|
|
role: {
|
|
type: String,
|
|
enum: ['parent', 'admin'],
|
|
default: 'parent'
|
|
},
|
|
isActive: {
|
|
type: Boolean,
|
|
default: true
|
|
},
|
|
preferences: {
|
|
notifications: {
|
|
type: Boolean,
|
|
default: true
|
|
},
|
|
language: {
|
|
type: String,
|
|
default: 'de'
|
|
},
|
|
theme: {
|
|
type: String,
|
|
enum: ['light', 'dark'],
|
|
default: 'light'
|
|
}
|
|
},
|
|
createdAt: {
|
|
type: Date,
|
|
default: Date.now
|
|
},
|
|
lastLogin: {
|
|
type: Date,
|
|
default: Date.now
|
|
}
|
|
});
|
|
|
|
// Passwort hashen vor dem Speichern
|
|
userSchema.pre('save', async function(next) {
|
|
// Nur hashen wenn Passwort geändert wurde
|
|
if (!this.isModified('password')) return next();
|
|
|
|
try {
|
|
// Passwort hashen
|
|
const salt = await bcrypt.genSalt(10);
|
|
this.password = await bcrypt.hash(this.password, salt);
|
|
next();
|
|
} catch (error) {
|
|
next(error);
|
|
}
|
|
});
|
|
|
|
// Methode zum Passwort-Vergleich
|
|
userSchema.methods.comparePassword = async function(candidatePassword) {
|
|
return await bcrypt.compare(candidatePassword, this.password);
|
|
};
|
|
|
|
// Virtuelle Eigenschaft für Kinder-Anzahl
|
|
userSchema.virtual('childrenCount', {
|
|
ref: 'Child',
|
|
localField: '_id',
|
|
foreignField: 'parent',
|
|
count: true
|
|
});
|
|
|
|
// JSON-Ausgabe anpassen (Passwort ausblenden)
|
|
userSchema.methods.toJSON = function() {
|
|
const user = this.toObject();
|
|
delete user.password;
|
|
return user;
|
|
};
|
|
|
|
module.exports = mongoose.model('User', userSchema); |