Guest User

Untitled

a guest
Apr 13th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. function checkUser(userData) {
  2. return User.find({ userName: userData.userName }).exec().then((user) => {
  3. if (user == undefined || user.length == 0) {
  4. throw new Error("Unable to find user: " + userData.userName);
  5. }
  6. else if (user[0].password != userData.password) {
  7. throw new Error("Incorrect Password for user: " + userData.userName);
  8. }
  9. else if (user[0].password == userData.password)
  10. {
  11. user[0].loginHistory.push({
  12. dateTime: (new Date()).toString(),
  13. userAgent: userData.userAgent});
  14. user[0].update({
  15. userName: userData.userName}, {$set: {loginHistory: user[0].loginHistory}}).exec().then((user) => {
  16. return user[0];
  17. })
  18. .catch((err) => {
  19. console.log(err);
  20. console.log(err.stack);
  21. throw new Error("There was an error verifying the user: ${err}");
  22. })
  23. }
  24. }).catch((err) => {
  25. console.log(err);
  26. console.log(err.stack);
  27. throw new Error("Unable to find user: " + err);
  28. });
  29. };
  30.  
  31. var mongoose = require("mongoose");
  32. var Schema = mongoose.Schema;
  33.  
  34. var userSchema = new Schema({
  35. "userName": {
  36. unique: true,
  37. type: String
  38. },
  39. "password": String,
  40. "email": String,
  41. "loginHistory": [{ "dateTime": Date, "userAgent": String }]
  42. });
  43.  
  44. let User; //to be defined on new connection
Add Comment
Please, Sign In to add comment