Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 33 additions & 6 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ import webpackDevMiddleware from 'webpack-dev-middleware';
import webpackHotMiddleware from 'webpack-hot-middleware';
import config from './webpack.config';
import userModel from './models/users';
var bodyParser = require('body-parser');

const app = express();

const compiler = webpack(config)
app.use(webpackDevMiddleware(compiler, { noInfo: true, publicPath: config.output.publicPath }))
app.use(webpackHotMiddleware(compiler))

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.listen(3000, ()=>{
console.log('Server started on port 3000');
});
Expand All @@ -23,13 +26,37 @@ app.get('/', (req, res) => {
res.sendFile(path.join(__dirname + '/client/src/index.html'));
});

app.post('/users/add',(req, res) => {
console.log(JSON.stringify(req.body, null, 2));
let userToAdd = new userModel({ username: req.body.username,password:req.body.password});
userToAdd.save(function (err, userToAdd) {
if (err) {
res.send('{"response":"error"}');
return console.error(err);
}else{
res.send('{"response":"success"}');
return console.log("userAdded");
}
});
});

app.get('/users/all',(req,res)=>{
userModel.find({}, function(err,o){
var arr = [];
for(var i in o){
arr.push(o[i]);
}
res.json(err?{error:err}:arr);
})
});

app.get('/dispute/options/:name', (req, res) => {
userModel.find({"username": new RegExp(req.params.name, "i")}, (err, users) => {
if(err) console.log('cant find');
res.send(users);
if(err){
console.log('Error when finding users');
}else{
res.send(users);
console.log(users);
}
});
// if (user) {
// console.log(user);
// ctx.body = {value: user[username], label: user[username]};
// };
});
2 changes: 1 addition & 1 deletion client/src/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
bottom: 0;
left: 0;
right: 0;
height: 50%;
height: 75%;
margin: auto;
};
47 changes: 45 additions & 2 deletions models/users.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,54 @@
import mongoose from '../db';

const Schema = mongoose.Schema;
import bcrypt from 'bcrypt'
const crypto = require('crypto');

let userSchema = new Schema({
username: {type: String, unique: true}
const userSchema = new Schema({
username: {
type: String,
unique: true,
required: true
},
password: {
type: String,
required: true
},
created: {
type: Date,
default: Date.now
},
isAdmin: {
type: Boolean,
default: false
}
});

userSchema.pre('save', function(next) {
var user = this;

if (!user.isModified('password')) return next();

bcrypt.genSalt(10, function(err, salt) { //10 - фактор соли,чем больше,тем лучше,походу
if (err) return next(err);

bcrypt.hash(user.password, salt, function(err, hash) {
if (err) return next(err);

user.password = hash;
next();
});
});
});

userSchema.methods.comparePassword = function(candidatePassword, cb) {
bcrypt.compare(candidatePassword, this.password, function(err, isMatch) {
if (err) return cb(err);
cb(null, isMatch);
});
};


let userModel = mongoose.model('user', userSchema);

export default userModel;
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"babel-cli": "^6.18.0",
"babel-core": "^6.18.2",
"babel-loader": "^6.2.7",
"babel-plugin-transform-object-rest-spread": "^6.19.0",
"babel-polyfill": "^6.16.0",
"babel-preset-latest": "^6.16.0",
"babel-preset-react": "^6.16.0",
Expand All @@ -28,6 +29,9 @@
"dependencies": {
"axios": "^0.15.2",
"babel-plugin-transform-object-rest-spread": "^6.19.0",
"babel-preset-es2015": "^6.18.0",
"babel-preset-react": "^6.16.0",
"bcrypt": "^0.8.7",
"body-parser": "^1.15.2",
"css-loader": "^0.25.0",
"express": "^4.14.0",
Expand Down