diff --git a/server/src/controllers/AuthenticationController.js b/server/src/controllers/AuthenticationController.js index 117cd07114b175270dceeefbe760a49960abcf9f..449a289c4b2c031db45022ec8cc989fcc029f470 100644 --- a/server/src/controllers/AuthenticationController.js +++ b/server/src/controllers/AuthenticationController.js @@ -12,15 +12,18 @@ function jwtSignUser (user) { module.exports={ async register(req, res){ - try{ + try{ const user= await User.create(req.body) - res.send(user.toJSON()) - }catch (err){ - res.status(400).send({ - error: 'This email account is already in use.' - }) + const userJson = user.toJSON() + res.send({ + user: userJSON, + token: jwtSignUser(userJson) + }) + } catch (err){ + res.status(400).send({ + error: 'This email account is already in use.' + }) } - }, async login(req, res) { try{ @@ -39,17 +42,17 @@ module.exports={ if(!isPasswordValid) { return res.status(403).send({ error: 'The login information was incorrect' - }) - } + }) + } const userJson = user.toJSON() res.send({ user: userJSON, token: jwtSignUser(userJson) }) - }catch (err){ - res.status(500).send({ - error: 'An error has occured trying to log in' - }) - } + }catch (err){ + res.status(500).send({ + error: 'An error has occured trying to log in' + }) + } } } \ No newline at end of file diff --git a/server/src/models/user.js b/server/src/models/user.js index e36ca96b8051927219bd2ee667f77fab6f0cc0f5..596ff8f5c349cbd308ac9316c7b724871a1039c7 100644 --- a/server/src/models/user.js +++ b/server/src/models/user.js @@ -1,10 +1,10 @@ const Promise = require('bluebird') -const bcrypt = Promise.promisifiyAll(require('bcrypt-nodejs')) +const bcrypt = Promise.promisifyAll(require('bcrypt-nodejs')) function hashPassword (user, options) { const SALT_FACTOR = 8 - if(!user.changed('password')){ + if(!user.changed('password')) { return; } @@ -14,12 +14,12 @@ function hashPassword (user, options) { .then(hash => { user.setDataValue('password', hash) }) - } +} -module.exports =(sequelize, DataTypes) =>{ - const User = sequelize.define('user',{ - email:{ - type:DataTypes.STRING, +module.exports = (sequelize, DataTypes) => { + const User = sequelize.define('User', { + email: { + type: DataTypes.STRING, unique: true }, password: DataTypes.STRING