-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
240c652
commit 981ed09
Showing
3 changed files
with
67 additions
and
4 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,60 @@ | ||
let mongoose = require('mongoose'); | ||
const mongoose = require('mongoose'); | ||
const validator = require('validator'); | ||
const jwt = require('jsonwebtoken'); | ||
const _ = require('lodash'); | ||
|
||
let User = mongoose.model('User', { | ||
var UserSchema = new mongoose.Schema({ | ||
email: { | ||
type: String, | ||
required: true, | ||
minlength: 5, | ||
trim: true, | ||
unique: true, | ||
validate: { | ||
validator: validator.isEmail, | ||
message: `{VALUE} is not a valid email.` | ||
} | ||
}, | ||
password: { | ||
type: String, | ||
minlength: 6, | ||
required: true, | ||
trim: true | ||
} | ||
}, | ||
tokens: [{ | ||
access: { | ||
type: String, | ||
required: true | ||
}, | ||
token: { | ||
type: String, | ||
required: true | ||
} | ||
}] | ||
}); | ||
|
||
UserSchema.methods.toJSON = function() { | ||
var user = this; | ||
var userObject = user.toObject(); | ||
|
||
return _.pick(userObject, ['_id','email']); | ||
} | ||
|
||
UserSchema.methods.generateAuthToken = function() { | ||
var user = this; | ||
var access = 'auth'; | ||
var token = jwt.sign({ | ||
_id: user._id.toHexString(), | ||
access | ||
}, 'abc123').toString(); | ||
|
||
user.tokens.push({ access, token }); | ||
|
||
return user.save().then(() => { | ||
return token; | ||
}) | ||
}; | ||
|
||
var User = mongoose.model('User', UserSchema); | ||
|
||
module.exports = { User }; |
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