From 39ff8c068dd0d7529c8341991a1b72c0bfd5dc9e Mon Sep 17 00:00:00 2001
From: V S Tharunika <1vstharu279@gmail.com>
Date: Sat, 2 Jan 2021 20:06:56 +0530
Subject: [PATCH] added token to register function

---
 .../controllers/AuthenticationController.js   | 31 ++++++++++---------
 server/src/models/user.js                     | 14 ++++-----
 2 files changed, 24 insertions(+), 21 deletions(-)

diff --git a/server/src/controllers/AuthenticationController.js b/server/src/controllers/AuthenticationController.js
index 117cd071..449a289c 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 e36ca96b..596ff8f5 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
-- 
GitLab