Advertisement
Guest User

Untitled

a guest
Mar 31st, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const mongoose = require('mongoose')
  2. const URLSlugs = require('mongoose-url-slugs');
  3. const bcrypt = require('bcryptjs');
  4.  
  5. // my schema goes here!
  6.  
  7. const Card = new mongoose.Schema({
  8.     cardname: {type:String,required:[true, 'Card name is required!']},
  9.     attack: String,
  10.     defense: String,
  11.     description: String,
  12. });
  13.  
  14. const Deck = new mongoose.Schema({
  15.     title: {type:String, required:[true, 'Deck name is required!']},
  16.     cardlist: [Card],
  17.     username: String,
  18. });
  19.  
  20. const User = new mongoose.Schema({
  21.     name: String,
  22.     username: {type:String, unique:true, required:true},
  23.     email: String,
  24.     passwordHash: {type:String, required:true},
  25. });
  26. //
  27. User.virtual('password')
  28.   .get(function() { return null; })
  29.   .set(function(value) {
  30.     const hash = bcrypt.hashSync(value, 10);
  31.     this.passwordHash = hash;
  32.   });
  33.  
  34. Card.statics.constructor = function(n,atk,def,des){
  35.     const Fakecard = mongoose.model('Card');
  36.     let returncard = new Fakecard;
  37.     returncard.cardname = n;
  38.     returncard.defense = def;
  39.     returncard.attack = atk;
  40.     returncard.description = des;
  41.     return returncard;
  42. }
  43.  
  44. Deck.statics.constructor = function(t,user){
  45.     const Fakedeck = mongoose.model('Deck');
  46.     let returndeck = new Fakedeck;
  47.     returndeck.title = t;
  48.     returndeck.username=user;
  49.     return returndeck;
  50. }
  51.  
  52. User.statics.constructor = function(n,user,email,pass){
  53.     const Fakeuser = mongoose.model('User');
  54.     let returnuser = new Fakeuser;
  55.     returnuser.name = n;
  56.     returnuser.username = user;
  57.     returnuser.email = email;
  58.     returnuser.password = pass;
  59.     return returnuser;
  60. }
  61. User.methods.checkPass = function(pass){
  62.  
  63.  
  64.   return bcrypt.compareSync(pass, this.passwordHash);
  65. }
  66. //https://github.com/Mallioch/node-express-passport-example/blob/master/models/user.js
  67. User.statics.authenticate = function(username, password, done) {
  68.  
  69.   this.findOne({ username: username }, function(err, user) {
  70.     if (err) {
  71.       console.log('Error attempting to use static authenticate function', err);
  72.       done(err);
  73.     }
  74.     else if (user && user.checkPass(password)) {
  75.       console.log('This should be a successful login.');
  76.       done(null, user);
  77.     }
  78.     else {
  79.       console.log('Probably got their password wrong');
  80.       done(null, false);
  81.     }
  82.   });
  83.  
  84.  
  85. }
  86.  
  87.  
  88. Deck.plugin(URLSlugs('title',{field: 'theslug'}));
  89.  
  90.  
  91. mongoose.model('Card', Card);
  92. mongoose.model('Deck', Deck);
  93. mongoose.model('User', User);
  94.  
  95. // is the environment variable, NODE_ENV, set to PRODUCTION?
  96. if (process.env.NODE_ENV === 'PRODUCTION') {
  97.  // if we're in PRODUCTION mode, then read the configration from a file
  98.  // use blocking file io to do this...
  99.  const fs = require('fs');
  100.  const path = require('path');
  101.  const fn = path.join(__dirname, 'config.json');
  102.  const data = fs.readFileSync(fn);
  103.  
  104.  // our configuration file will be in json, so parse it and set the
  105.  // conenction string appropriately!
  106.  const conf = JSON.parse(data);
  107.  let dbconf = conf.dbconf;
  108.  mongoose.connect(dbconf);
  109. } else {
  110.  // if we're not in PRODUCTION mode, then use
  111.  dbconf = 'mongodb://localhost/final-project';
  112.  mongoose.connect(dbconf);
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement