Skip to content

Instantly share code, notes, and snippets.

@oakinogundeji
Last active April 3, 2023 10:15
Show Gist options
  • Save oakinogundeji/bc1259a30233db082c5f91b1f582f8ff to your computer and use it in GitHub Desktop.
Save oakinogundeji/bc1259a30233db082c5f91b1f582f8ff to your computer and use it in GitHub Desktop.
user model with refs to album collection
'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