Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ====SERVER====
- *config.js*
- const Sequelize = require('sequelize');
- const Op = Sequelize.Op;
- module.exports = {
- port: process.env.PORT || 8081,
- db: {
- database: process.env.SQL_DATABASE || 'blah',
- user: process.env.SQL_USER || 'blah',
- password: process.env.SQL_PASS || 'blah',
- options: {
- dialect: process.env.DIALECT || 'sqlite',
- host: process.env.HOST || 'localhost',
- operatorsAliases: Op
- }
- },
- authentication: {
- jwtSecret: process.env.JWT_SECRET || 'secret'
- }
- };
- *index.js*
- const fs = require('fs');
- const path = require('path');
- const Sequelize = require('sequelize');
- const config = require('../config/config');
- const db = {};
- const sequelize = new Sequelize(
- config.db.database,
- config.db.user,
- config.db.password,
- config.db.options
- );
- fs
- .readdirSync(__dirname)
- .filter((file) =>
- file !== 'index.js'
- )
- .forEach((file) => {
- const model = sequelize.import(path.join(__dirname, file));
- db[model.name] = model;
- });
- db.sequelize = sequelize;
- db.Sequelize = Sequelize;
- module.exports = db;
- *User.js*
- // const Promise = require('bluebird');
- const bcrypt = (require('bcrypt'));
- function hashPassword (user) {
- const SALT_ROUNDS = 11;
- if (!user.changed('password')) {
- return;
- }
- return bcrypt
- .genSalt(SALT_ROUNDS)
- .then(salt => bcrypt.hash(user.password, salt, null))
- .then(hash => {
- user.setDataValue('password', hash);
- });
- }
- module.exports = (sequelize, DataTypes) => {
- const User = sequelize.define('User', {
- email: {
- type: DataTypes.STRING,
- unique: true
- },
- password: DataTypes.STRING,
- profilePic: DataTypes.STRING
- }, {
- hooks: {
- beforeSave: hashPassword
- }
- }
- );
- User.prototype.comparePassword = function (password) {
- return bcrypt.compare(password, this.password);
- };
- return User;
- };
- *UserController.js*
- const {User} = require('../models');
- //call authentication endpoints are declared here
- module.exports = {
- async put (req, res) {
- try {
- req.body.profilePic = req.file.path;
- // req.body.profilePic = 'test';
- await User.update(req.body, {
- where: {
- email: req.body.email
- }
- });
- res.send(req.body);
- } catch (err) {
- res.status(500).send({
- error: 'An error occured trying to update the user profile.'
- });
- }
- }
- };
- *routes.js*
- //this file points to controllers
- const AuthenticationController = require('./controllers/AuthenticationController');
- const AuthenticationControllerPolicy = require('./policies/AuthenticationController');
- const UserController = require('./controllers/UserController');
- const ScriptController = require('./controllers/ScriptController');
- // const ProfilePic = require('./fileHandlers/ProfilePic');
- // TODO: move this into external file
- const multer = require('multer');
- const storage = multer.diskStorage({
- destination: (req, file, cb) => {
- cb(null, './profilePictures/');
- },
- filename: (req, file, cb) => {
- const fileFormat = (file.originalname).split('.');
- const fileName = `${req.body.email}.${fileFormat[fileFormat.length - 1]}`.toLowerCase();
- cb(null, fileName);
- }
- });
- const fileFilter = (req, file, cb) => {
- if (file.mimetype === 'image/jpeg' || file.mimetype === 'image/png') {
- cb (null, true);
- } else {
- cb(new Error('Please select a .jpg or .png image.'));
- }
- };
- const upload = multer({
- storage,
- limits: {
- fileSize: 1024 * 1024 * 5
- },
- fileFilter
- });
- /////////////////////////////////////////////
- module.exports = (app) => {
- app.post('/register' ,
- AuthenticationControllerPolicy.register,
- AuthenticationController.register
- );
- app.post('/login' ,
- AuthenticationController.login
- );
- app.put('/users/',
- // ProfilePic.uploadPhoto,
- upload.single('file'),
- UserController.put
- );
- app.get('/scripts/user/:user',
- ScriptController.userIndex
- );
- app.get('/scripts/:scriptId',
- ScriptController.show
- );
- app.post('/scripts/',
- ScriptController.post
- );
- app.put('/scripts/:scriptId',
- ScriptController.put
- );
- app.delete('/scripts/:scriptId',
- ScriptController.delete
- );
- };
- ====CLIENT====
- *Api.js*
- import axios from 'axios';
- export default () => {
- return axios.create({
- baseURL: process.env.BASE_URL || 'http://localhost:8081' // TODO: add Authorization
- });
- }
- *UserService*
- import Api from '@/services/Api'
- export default {
- put(data) {
- return Api().put (`users/`,
- data,
- {
- headers: {
- 'Content-Type': 'multipart/form-data',
- 'Cache-Control': 'no-cache'
- }
- }
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement