Skip to content

Commit

Permalink
Create User.js
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Nov 22, 2024
1 parent 424edbf commit a08cca1
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions blockchain_integration/pi_network/models/User.js
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit a08cca1

Please sign in to comment.