diff --git a/blockchain_integration/pi_network/models/User.js b/blockchain_integration/pi_network/models/User.js new file mode 100644 index 000000000..141893b64 --- /dev/null +++ b/blockchain_integration/pi_network/models/User.js @@ -0,0 +1,50 @@ +const mongoose = require('mongoose'); +const bcrypt = require('bcrypt'); + +const userSchema = new mongoose.Schema({ + username: { + type: String, + required: true, + unique: true, + trim: true, + }, + email: { + type: String, + required: true, + unique: true, + trim: true, + lowercase: true, + }, + password: { + type: String, + required: true, + }, + role: { + type: String, + enum: ['user', 'admin'], + default: 'user', + }, + createdAt: { + type: Date, + default: Date.now, + }, + lastLogin: { + type: Date, + }, +}); + +// Hash the password before saving +userSchema.pre('save', async function (next) { + if (this.isModified('password')) { + this.password = await bcrypt.hash(this.password, 10); + } + next(); +}); + +// Method to compare passwords +userSchema.methods.comparePassword = async function (password) { + return await bcrypt.compare(password, this.password); +}; + +const User = mongoose.model('User ', userSchema); +module.exports = User;