Advertisement
Guest User

Untitled

a guest
Jun 16th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. let userSchema = new mongoose.Schema({
  2. username: {type: String, required: true},
  3. email: {type: String, required: true, index: {unique: true}},
  4. joinDate: {type: Date, default: Date.now},
  5. clips: [clipSchema],
  6. hash: {type: String},
  7. salt: {type: String}
  8. })
  9.  
  10. userSchema.methods.setPassword = (password) => {
  11. this.salt = crypto.randomBytes(32).toString('hex')
  12. this.hash = crypto.pbkdf2Sync(password, this.salt, 100000, 512, 'sha512').toString('hex')
  13. }
  14.  
  15. let user = new User()
  16.  
  17. user.username = req.body.username
  18. user.email = req.body.email
  19. user.setPassword(req.body.password)
  20.  
  21. user.save((err) => {
  22. if (err) {
  23. sendJsonResponse(res, 404, err)
  24. } else {
  25. let token = user.generateJwt()
  26. sendJsonResponse(res, 200, { 'token': token })
  27. }
  28. })
  29.  
  30. {
  31. "_id" : ObjectId("576338b363bb7df7024c044b"),
  32. "email" : "boss@potato.com",
  33. "username" : "Bob",
  34. "clips" : [ ],
  35. "joinDate" : ISODate("2016-06-16T23:39:31.825Z"),
  36. "__v" : 0
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement