Guest User

Untitled

a guest
Aug 11th, 2018
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.75 KB | None | 0 0
  1. const { response } = require('../helpers/index');
  2. const { users: User } = require('../models/index');
  3. const axios = require('axios');
  4. const config = require('../config/config');
  5. const jwtS = require('../config/configJwt')
  6. const jwt = require('jsonwebtoken')
  7. const Sequelize = require('sequelize');
  8. const { Op } = Sequelize;
  9.  
  10. const jwtSignUser = (user) => {
  11. const ONE_WEEK = 60 * 60 * 24 * 7
  12. return jwt.sign(user, jwtS.authentication.jwtSecret, {
  13. expiresIn: ONE_WEEK
  14. })
  15. }
  16. // console.log('===================>' + jwtSignUser)
  17.  
  18.  
  19. // const list = {
  20. // find: async (req, res) => {
  21. // try {
  22. // const user = await User.all();
  23. // return res
  24. // .status(200)
  25. // .json(response(true, 'User retrieved successfully', user, null));
  26. // } catch (error) {
  27. // if (error.errors) {
  28. // return res.status(400).json(response(false, error.errors));
  29. // }
  30. // return res.status(400).json(response(false, error.message));
  31. // }
  32. // },
  33. // }
  34. module.exports = {
  35. async register(req, res) {
  36. try {
  37. const user = await User.create(req.body)
  38. const userJson = user.toJSON()
  39. res.send({
  40. user: userJson,
  41. token: jwtSignUser(userJson)
  42. })
  43. }
  44. catch (err) {
  45. res.status(400).send({
  46. error: err
  47.  
  48. })
  49. }
  50. },
  51.  
  52. async login(req, res) {
  53. try {
  54. const { email, password } = req.body
  55. const user = await User.findOne({
  56. where: {
  57. email: email
  58. }
  59. })
  60.  
  61. if (!user) {
  62. return res.status(403).send({
  63. error: err + 'The login user information was incorrect'
  64. })
  65. }
  66. const isPasswordValid = await user.comparePassword(password)
  67. if (!isPasswordValid) {
  68. console.log(isPasswordValid)
  69. return res.status(403).send({
  70. error: 'The login password information was incorrect'
  71. })
  72. }
  73.  
  74. const userJson = user.toJSON()
  75. res.send({
  76. user: userJson,
  77. token: jwtSignUser(userJson)
  78. })
  79. } catch (err) {
  80. res.status(500).send({
  81. error: 'An error has occured trying to log in'
  82. })
  83. }
  84.  
  85. }
  86. }
  87.  
  88. 'use strict';
  89. const Promise = require('bluebird')
  90. const bcrypt = Promise.promisifyAll(require("bcrypt-nodejs"))
  91.  
  92. function hashPassword(user, options) {
  93. const SALT_FACTOR = 8
  94.  
  95. if (!user.changed("password")) {
  96. return
  97. }
  98.  
  99. return bcrypt
  100. .genSaltAsync(SALT_FACTOR)
  101. .then(salt => bcrypt.hashSync(user.password, salt, null))
  102. .then(hash => {
  103. user.setDataValue('password', hash)
  104. })
  105. }
  106. module.exports = (sequelize, DataTypes) => {
  107. const User = sequelize.define('users', {
  108. full_name: {
  109. type: DataTypes.STRING,
  110. allowNull: false,
  111. },
  112. email: {
  113. type: DataTypes.STRING,
  114. allowNull: false,
  115. unique : true,
  116. },
  117. password: {
  118. type: DataTypes.STRING,
  119. allowNull: false,
  120. },
  121. phone: {
  122. type: DataTypes.STRING,
  123. allowNull: false,
  124. },
  125. }, {
  126. hooks :{
  127. beforeCreate: hashPassword,
  128. beforeUpdate: hashPassword,
  129. beforeSave:hashPassword
  130. }
  131. });
  132.  
  133. User.prototype.comparePassword = function(password){
  134. return bcrypt.compareSync(password, this.password)
  135. }
  136.  
  137. User.associate = (models) => {
  138. // User.hasOne(models.access_tokens, {
  139. // foreignKey: 'user_id',
  140. // onDelete: 'CASCADE'
  141. // });
  142. User.hasMany(models.authors, {
  143. foreignKey: 'user_id',
  144. onDelete: 'CASCADE'
  145. });
  146. };
  147.  
  148. return User;
  149. };
Add Comment
Please, Sign In to add comment