Advertisement
Guest User

Untitled

a guest
Aug 15th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ====SERVER====
  2.  
  3. *config.js*
  4.  
  5. const Sequelize = require('sequelize');
  6. const Op = Sequelize.Op;
  7.  
  8. module.exports = {
  9.     port: process.env.PORT || 8081,
  10.     db: {
  11.         database: process.env.SQL_DATABASE || 'blah',
  12.         user: process.env.SQL_USER || 'blah',
  13.         password: process.env.SQL_PASS || 'blah',
  14.         options: {
  15.             dialect: process.env.DIALECT || 'sqlite',
  16.             host: process.env.HOST || 'localhost',
  17.             operatorsAliases: Op
  18.         }
  19.     },
  20.     authentication: {
  21.         jwtSecret: process.env.JWT_SECRET || 'secret'
  22.     }
  23. };
  24.  
  25.  
  26.  
  27. *index.js*
  28.  
  29. const fs = require('fs');
  30. const path = require('path');
  31. const Sequelize = require('sequelize');
  32. const config = require('../config/config');
  33. const db = {};
  34.  
  35. const sequelize = new Sequelize(
  36.     config.db.database,
  37.     config.db.user,
  38.     config.db.password,
  39.     config.db.options
  40. );
  41.  
  42. fs
  43.     .readdirSync(__dirname)
  44.     .filter((file) =>
  45.         file !== 'index.js'
  46.     )
  47.     .forEach((file) => {
  48.         const model = sequelize.import(path.join(__dirname, file));
  49.         db[model.name] = model;
  50.     });
  51.  
  52. db.sequelize = sequelize;
  53. db.Sequelize = Sequelize;
  54.  
  55. module.exports = db;
  56.  
  57.  
  58. *User.js*
  59.  
  60. // const Promise = require('bluebird');
  61. const bcrypt = (require('bcrypt'));
  62.  
  63. function hashPassword (user) {
  64.     const SALT_ROUNDS = 11;
  65.  
  66.     if (!user.changed('password')) {
  67.         return;
  68.     }
  69.  
  70.     return bcrypt
  71.         .genSalt(SALT_ROUNDS)
  72.         .then(salt => bcrypt.hash(user.password, salt, null))
  73.         .then(hash => {
  74.             user.setDataValue('password', hash);
  75.         });
  76. }
  77.  
  78. module.exports = (sequelize, DataTypes) => {
  79.     const User = sequelize.define('User', {
  80.         email: {
  81.             type: DataTypes.STRING,
  82.             unique: true
  83.         },
  84.         password: DataTypes.STRING,
  85.         profilePic: DataTypes.STRING
  86.     }, {
  87.         hooks: {
  88.             beforeSave: hashPassword
  89.         }
  90.     }
  91.  
  92.     );
  93.  
  94.     User.prototype.comparePassword = function (password) {
  95.         return bcrypt.compare(password, this.password);
  96.     };
  97.  
  98.     return User;
  99. };
  100.  
  101.  
  102. *UserController.js*
  103.  
  104. const {User} = require('../models');
  105.  
  106. //call authentication endpoints are declared here
  107. module.exports = {
  108.     async put (req, res) {
  109.         try {
  110.             req.body.profilePic = req.file.path;
  111.             // req.body.profilePic = 'test';
  112.             await User.update(req.body, {
  113.                 where: {
  114.                     email: req.body.email
  115.                 }
  116.             });
  117.             res.send(req.body);
  118.         } catch (err) {
  119.             res.status(500).send({
  120.                 error: 'An error occured trying to update the user profile.'
  121.             });
  122.         }
  123.     }
  124. };
  125.  
  126.  
  127. *routes.js*
  128.  
  129. //this file points to controllers
  130. const AuthenticationController = require('./controllers/AuthenticationController');
  131. const AuthenticationControllerPolicy = require('./policies/AuthenticationController');
  132. const UserController = require('./controllers/UserController');
  133. const ScriptController = require('./controllers/ScriptController');
  134. // const ProfilePic = require('./fileHandlers/ProfilePic');
  135.  
  136.  
  137. // TODO: move this into external file
  138. const multer = require('multer');
  139. const storage = multer.diskStorage({
  140.     destination: (req, file, cb) => {
  141.         cb(null, './profilePictures/');
  142.     },
  143.     filename: (req, file, cb) => {
  144.         const fileFormat = (file.originalname).split('.');
  145.         const fileName = `${req.body.email}.${fileFormat[fileFormat.length - 1]}`.toLowerCase();
  146.         cb(null, fileName);
  147.     }
  148. });
  149.  
  150. const fileFilter = (req, file, cb) => {
  151.     if (file.mimetype === 'image/jpeg' || file.mimetype === 'image/png') {
  152.         cb (null, true);
  153.     } else {
  154.         cb(new Error('Please select a .jpg or .png image.'));
  155.     }
  156. };
  157.  
  158. const upload = multer({
  159.     storage,
  160.     limits: {
  161.         fileSize: 1024 * 1024 * 5
  162.     },
  163.     fileFilter
  164. });
  165. /////////////////////////////////////////////
  166.  
  167.  
  168. module.exports = (app) => {
  169.     app.post('/register' ,
  170.         AuthenticationControllerPolicy.register,
  171.         AuthenticationController.register
  172.     );
  173.    
  174.     app.post('/login' ,
  175.         AuthenticationController.login
  176.     );
  177.  
  178.     app.put('/users/',
  179.         // ProfilePic.uploadPhoto,
  180.         upload.single('file'),
  181.         UserController.put
  182.     );
  183.  
  184.     app.get('/scripts/user/:user',
  185.         ScriptController.userIndex
  186.     ); 
  187.  
  188.     app.get('/scripts/:scriptId',
  189.         ScriptController.show
  190.     );
  191.  
  192.     app.post('/scripts/',
  193.         ScriptController.post
  194.     );
  195.  
  196.     app.put('/scripts/:scriptId',
  197.         ScriptController.put
  198.     );
  199.  
  200.     app.delete('/scripts/:scriptId',
  201.         ScriptController.delete
  202.     );
  203. };
  204.  
  205.  
  206. ====CLIENT====
  207.  
  208. *Api.js*
  209. import axios from 'axios';
  210.  
  211. export default () => {
  212.     return axios.create({
  213.         baseURL: process.env.BASE_URL || 'http://localhost:8081'    //  TODO: add Authorization
  214.     });
  215. }
  216.  
  217. *UserService*
  218.  
  219. import Api from '@/services/Api'
  220.  
  221. export default {
  222.     put(data) {
  223.         return Api().put (`users/`,
  224.             data,
  225.             {
  226.                 headers: {
  227.                     'Content-Type': 'multipart/form-data',
  228.                     'Cache-Control': 'no-cache'
  229.                 }
  230.             }
  231.         );
  232.     }
  233. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement