-
-
Save oakinogundeji/bc1259a30233db082c5f91b1f582f8ff to your computer and use it in GitHub Desktop.
user model with refs to album collection
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 | |
}, | |
album: [{ | |
type: Schema.Types.ObjectId, | |
ref: 'Album' | |
}] | |
}); | |
/** | |
* 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); | |
}; | |
/** | |
* 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