-
-
Save oakinogundeji/dd36aa05972fb5d59b3dc3bd5834891d to your computer and use it in GitHub Desktop.
naive user model
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
/** | |
* Module dependencies | |
*/ | |
const | |
Mongoose = require('mongoose'), | |
argon2 = require('argon2'); | |
/** | |
* User Schema | |
*/ | |
const UserSchema = new Mongoose.Schema({ | |
username: { | |
type: String, | |
required: true, | |
unique: true | |
}, | |
email: { | |
type: String, | |
required: true, | |
unique: true | |
}, | |
password: { | |
type: String, | |
required: true | |
}, | |
hasPhotos: { | |
type: Boolean, | |
required: true, | |
default: false | |
}, | |
photos: [{ | |
filename: { | |
type: String, | |
default: '' | |
}, | |
createdAt: { | |
type: Date, | |
default: Date.now, | |
required: true | |
}, | |
fullsize: { | |
type: String | |
}, | |
thumbnail: { | |
type: String | |
} | |
}] | |
}); | |
/** | |
* User Schema Methods | |
*/ | |
UserSchema.methods.generateHash = async function (password) { | |
return await argon2.hash(password); | |
}; | |
UserSchema.methods.validatePassword = async function (candidate) { | |
return await argon2.verify(this.password, candidate); | |
}; | |
/** | |
* Create Schema Secondary Indexes | |
*/ | |
UserSchema.index({"photos.filename": 1}); | |
/** | |
* Compile Schema to Model | |
*/ | |
const UserModel = Mongoose.model('User', UserSchema); | |
/** | |
* Export UserModel | |
*/ | |
module.exports = UserModel; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment