Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const { response } = require('../helpers/index');
- const { users: User } = require('../models/index');
- const axios = require('axios');
- const config = require('../config/config');
- const jwtS = require('../config/configJwt')
- const jwt = require('jsonwebtoken')
- const Sequelize = require('sequelize');
- const { Op } = Sequelize;
- const jwtSignUser = (user) => {
- const ONE_WEEK = 60 * 60 * 24 * 7
- return jwt.sign(user, jwtS.authentication.jwtSecret, {
- expiresIn: ONE_WEEK
- })
- }
- // console.log('===================>' + jwtSignUser)
- // const list = {
- // find: async (req, res) => {
- // try {
- // const user = await User.all();
- // return res
- // .status(200)
- // .json(response(true, 'User retrieved successfully', user, null));
- // } catch (error) {
- // if (error.errors) {
- // return res.status(400).json(response(false, error.errors));
- // }
- // return res.status(400).json(response(false, error.message));
- // }
- // },
- // }
- module.exports = {
- async register(req, res) {
- try {
- const user = await User.create(req.body)
- const userJson = user.toJSON()
- res.send({
- user: userJson,
- token: jwtSignUser(userJson)
- })
- }
- catch (err) {
- res.status(400).send({
- error: err
- })
- }
- },
- async login(req, res) {
- try {
- const { email, password } = req.body
- const user = await User.findOne({
- where: {
- email: email
- }
- })
- if (!user) {
- return res.status(403).send({
- error: err + 'The login user information was incorrect'
- })
- }
- const isPasswordValid = await user.comparePassword(password)
- if (!isPasswordValid) {
- console.log(isPasswordValid)
- return res.status(403).send({
- error: 'The login password 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'
- })
- }
- }
- }
- 'use strict';
- const Promise = require('bluebird')
- const bcrypt = Promise.promisifyAll(require("bcrypt-nodejs"))
- function hashPassword(user, options) {
- const SALT_FACTOR = 8
- if (!user.changed("password")) {
- return
- }
- return bcrypt
- .genSaltAsync(SALT_FACTOR)
- .then(salt => bcrypt.hashSync(user.password, salt, null))
- .then(hash => {
- user.setDataValue('password', hash)
- })
- }
- module.exports = (sequelize, DataTypes) => {
- const User = sequelize.define('users', {
- full_name: {
- type: DataTypes.STRING,
- allowNull: false,
- },
- email: {
- type: DataTypes.STRING,
- allowNull: false,
- unique : true,
- },
- password: {
- type: DataTypes.STRING,
- allowNull: false,
- },
- phone: {
- type: DataTypes.STRING,
- allowNull: false,
- },
- }, {
- hooks :{
- beforeCreate: hashPassword,
- beforeUpdate: hashPassword,
- beforeSave:hashPassword
- }
- });
- User.prototype.comparePassword = function(password){
- return bcrypt.compareSync(password, this.password)
- }
- User.associate = (models) => {
- // User.hasOne(models.access_tokens, {
- // foreignKey: 'user_id',
- // onDelete: 'CASCADE'
- // });
- User.hasMany(models.authors, {
- foreignKey: 'user_id',
- onDelete: 'CASCADE'
- });
- };
- return User;
- };
Add Comment
Please, Sign In to add comment