Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const mongoose = require('mongoose')
- const URLSlugs = require('mongoose-url-slugs');
- const bcrypt = require('bcryptjs');
- // my schema goes here!
- const Card = new mongoose.Schema({
- cardname: {type:String,required:[true, 'Card name is required!']},
- attack: String,
- defense: String,
- description: String,
- });
- const Deck = new mongoose.Schema({
- title: {type:String, required:[true, 'Deck name is required!']},
- cardlist: [Card],
- username: String,
- });
- const User = new mongoose.Schema({
- name: String,
- username: {type:String, unique:true, required:true},
- email: String,
- passwordHash: {type:String, required:true},
- });
- //
- User.virtual('password')
- .get(function() { return null; })
- .set(function(value) {
- const hash = bcrypt.hashSync(value, 10);
- this.passwordHash = hash;
- });
- Card.statics.constructor = function(n,atk,def,des){
- const Fakecard = mongoose.model('Card');
- let returncard = new Fakecard;
- returncard.cardname = n;
- returncard.defense = def;
- returncard.attack = atk;
- returncard.description = des;
- return returncard;
- }
- Deck.statics.constructor = function(t,user){
- const Fakedeck = mongoose.model('Deck');
- let returndeck = new Fakedeck;
- returndeck.title = t;
- returndeck.username=user;
- return returndeck;
- }
- User.statics.constructor = function(n,user,email,pass){
- const Fakeuser = mongoose.model('User');
- let returnuser = new Fakeuser;
- returnuser.name = n;
- returnuser.username = user;
- returnuser.email = email;
- returnuser.password = pass;
- return returnuser;
- }
- User.methods.checkPass = function(pass){
- return bcrypt.compareSync(pass, this.passwordHash);
- }
- //https://github.com/Mallioch/node-express-passport-example/blob/master/models/user.js
- User.statics.authenticate = function(username, password, done) {
- this.findOne({ username: username }, function(err, user) {
- if (err) {
- console.log('Error attempting to use static authenticate function', err);
- done(err);
- }
- else if (user && user.checkPass(password)) {
- console.log('This should be a successful login.');
- done(null, user);
- }
- else {
- console.log('Probably got their password wrong');
- done(null, false);
- }
- });
- }
- Deck.plugin(URLSlugs('title',{field: 'theslug'}));
- mongoose.model('Card', Card);
- mongoose.model('Deck', Deck);
- mongoose.model('User', User);
- // is the environment variable, NODE_ENV, set to PRODUCTION?
- if (process.env.NODE_ENV === 'PRODUCTION') {
- // if we're in PRODUCTION mode, then read the configration from a file
- // use blocking file io to do this...
- const fs = require('fs');
- const path = require('path');
- const fn = path.join(__dirname, 'config.json');
- const data = fs.readFileSync(fn);
- // our configuration file will be in json, so parse it and set the
- // conenction string appropriately!
- const conf = JSON.parse(data);
- let dbconf = conf.dbconf;
- mongoose.connect(dbconf);
- } else {
- // if we're not in PRODUCTION mode, then use
- dbconf = 'mongodb://localhost/final-project';
- mongoose.connect(dbconf);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement