Skip to content

Commit

Permalink
Saves user in database
Browse files Browse the repository at this point in the history
  • Loading branch information
Remchi committed Jul 19, 2016
1 parent a753e5e commit 55d29c0
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 1 deletion.
53 changes: 53 additions & 0 deletions knexfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Update with your config settings.

module.exports = {

development: {
client: 'postgresql',
connection: {
database: 'reddice',
user: 'rem',
password: ''
},
pool: {
min: 2,
max: 10
},
migrations: {
tableName: 'knex_migrations'
}
},

staging: {
client: 'postgresql',
connection: {
database: 'my_db',
user: 'username',
password: 'password'
},
pool: {
min: 2,
max: 10
},
migrations: {
tableName: 'knex_migrations'
}
},

production: {
client: 'postgresql',
connection: {
database: 'my_db',
user: 'username',
password: 'password'
},
pool: {
min: 2,
max: 10
},
migrations: {
tableName: 'knex_migrations'
}
}

};
15 changes: 15 additions & 0 deletions migrations/20160719130655_users.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

exports.up = function(knex, Promise) {
return knex.schema.createTable('users', function(table) {
table.increments();
table.string('username').notNullable().unique();
table.string('email').notNullable().unique();
table.string('timezone').notNullable();
table.string('password_digest').notNullable();
table.timestamps();
});
};

exports.down = function(knex, Promise) {
return knex.schema.dropTable('users');
};
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@
},
"dependencies": {
"axios": "^0.12.0",
"bcrypt": "^0.8.7",
"body-parser": "^1.15.2",
"bookshelf": "^0.10.0",
"classnames": "^2.2.5",
"express": "^4.14.0",
"knex": "^0.11.7",
"lodash": "^4.13.1",
"pg": "^6.0.2",
"react": "^15.1.0",
"react-dom": "^15.1.0",
"react-redux": "^4.4.5",
Expand Down
5 changes: 5 additions & 0 deletions server/bookshelf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import knex from 'knex';
import bookshelf from 'bookshelf';
import knexConfig from '../knexfile';

export default bookshelf(knex(knexConfig.development));
5 changes: 5 additions & 0 deletions server/models/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import bookshelf from '../bookshelf';

export default bookshelf.Model.extend({
tableName: 'users'
});
13 changes: 12 additions & 1 deletion server/routes/users.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import express from 'express';
import validateInput from '../shared/validations/signup';
import bcrypt from 'bcrypt';

import User from '../models/user';

let router = express.Router();

Expand All @@ -8,7 +11,15 @@ router.post('/', (req, res) => {
const { errors, isValid } = validateInput(req.body);

if (isValid) {
res.json({ success: true });
const { username, password, timezone, email } = req.body;
const password_digest = bcrypt.hashSync(password, 10);

User.forge({
username, timezone, email, password_digest
}, { hasTimestamps: true }).save()
.then(user => res.json({ success: true }))
.catch(err => res.status(500).json({ error: err }));

} else {
res.status(400).json(errors);
}
Expand Down

0 comments on commit 55d29c0

Please sign in to comment.