Spaces:
Running
Running
Download models/User.js from bravo09/brainboost-academy: direct link, hf CLI and curl.
- Browser
- Download file 1.02 kB
-
https://huggingface.co/spaces/bravo09/brainboost-academy/resolve/main/models/User.js
- Command line
-
hf download hf://spaces/bravo09/brainboost-academy/models/User.js
-
curl -L -o User.js https://huggingface.co/spaces/bravo09/brainboost-academy/resolve/main/models/User.js
1.02 kB
| const mongoose = require('mongoose'); | |
| const bcrypt = require('bcryptjs'); | |
| const UserSchema = new mongoose.Schema({ | |
| name: { | |
| type: String, | |
| required: true | |
| }, | |
| email: { | |
| type: String, | |
| required: true, | |
| unique: true | |
| }, | |
| password: { | |
| type: String, | |
| required: true | |
| }, | |
| role: { | |
| type: String, | |
| enum: ['student', 'instructor', 'admin'], | |
| default: 'student' | |
| }, | |
| enrolledCourses: [{ | |
| type: mongoose.Schema.Types.ObjectId, | |
| ref: 'Course' | |
| }], | |
| createdAt: { | |
| type: Date, | |
| default: Date.now | |
| } | |
| }); | |
| // Hash password before saving | |
| UserSchema.pre('save', async function(next) { | |
| if (!this.isModified('password')) { | |
| return next(); | |
| } | |
| const salt = await bcrypt.genSalt(10); | |
| this.password = await bcrypt.hash(this.password, salt); | |
| next(); | |
| }); | |
| // Method to compare passwords | |
| UserSchema.methods.matchPassword = async function(enteredPassword) { | |
| return await bcrypt.compare(enteredPassword, this.password); | |
| }; | |
| module.exports = mongoose.model('User', UserSchema); |