├── static └── .gitkeep ├── config ├── settings.js ├── prod.env.js ├── test.env.js ├── dev.env.js ├── passport.js └── index.js ├── .eslintignore ├── test ├── unit │ ├── setup.js │ ├── .eslintrc │ ├── specs │ │ └── HelloWorld.spec.js │ └── jest.conf.js └── e2e │ ├── specs │ └── test.js │ ├── custom-assertions │ └── elementCount.js │ ├── nightwatch.conf.js │ └── runner.js ├── src ├── assets │ └── logo.png ├── App.vue ├── main.js ├── router │ └── index.js └── components │ ├── BookList.vue │ ├── Register.vue │ ├── Login.vue │ └── HelloWorld.vue ├── .editorconfig ├── .gitignore ├── .postcssrc.js ├── index.html ├── models ├── Book.js └── User.js ├── README.md ├── .babelrc ├── .eslintrc.js ├── LICENSE ├── app.js ├── routes ├── auth.js └── book.js ├── bin └── www └── package.json /static/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /config/settings.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'secret':'mevnsecure' 3 | }; 4 | -------------------------------------------------------------------------------- /.eslintignore: -------------------------------------------------------------------------------- 1 | /build/ 2 | /config/ 3 | /dist/ 4 | /*.js 5 | /test/unit/coverage/ 6 | -------------------------------------------------------------------------------- /test/unit/setup.js: -------------------------------------------------------------------------------- 1 | import Vue from 'vue' 2 | 3 | Vue.config.productionTip = false 4 | -------------------------------------------------------------------------------- /config/prod.env.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | module.exports = { 3 | NODE_ENV: '"production"' 4 | } 5 | -------------------------------------------------------------------------------- /src/assets/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/didinj/mevn-stack-vue-2/HEAD/src/assets/logo.png -------------------------------------------------------------------------------- /test/unit/.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "env": { 3 | "jest": true 4 | }, 5 | "globals": { 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /config/test.env.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | const merge = require('webpack-merge') 3 | const devEnv = require('./dev.env') 4 | 5 | module.exports = merge(devEnv, { 6 | NODE_ENV: '"testing"' 7 | }) 8 | -------------------------------------------------------------------------------- /config/dev.env.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | const merge = require('webpack-merge') 3 | const prodEnv = require('./prod.env') 4 | 5 | module.exports = merge(prodEnv, { 6 | NODE_ENV: '"development"' 7 | }) 8 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | charset = utf-8 5 | indent_style = space 6 | indent_size = 2 7 | end_of_line = lf 8 | insert_final_newline = true 9 | trim_trailing_whitespace = true 10 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules/ 3 | /dist/ 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | /test/unit/coverage/ 8 | /test/e2e/reports/ 9 | selenium-debug.log 10 | 11 | # Editor directories and files 12 | .idea 13 | .vscode 14 | *.suo 15 | *.ntvs* 16 | *.njsproj 17 | *.sln 18 | -------------------------------------------------------------------------------- /.postcssrc.js: -------------------------------------------------------------------------------- 1 | // https://github.com/michael-ciniawsky/postcss-load-config 2 | 3 | module.exports = { 4 | "plugins": { 5 | "postcss-import": {}, 6 | "postcss-url": {}, 7 | // to edit target browsers: use "browserslist" field in package.json 8 | "autoprefixer": {} 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | mevn-secure 7 | 8 | 9 |
10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /models/Book.js: -------------------------------------------------------------------------------- 1 | var mongoose = require('mongoose'); 2 | 3 | var BookSchema = new mongoose.Schema({ 4 | isbn: String, 5 | title: String, 6 | author: String, 7 | description: String, 8 | published_year: String, 9 | publisher: String, 10 | updated_date: { type: Date, default: Date.now }, 11 | }); 12 | 13 | module.exports = mongoose.model('Book', BookSchema); 14 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Securing MEVN Stack (Vue.js 2) Web Application using Passport 2 | 3 | This source code is part of [Securing MEVN Stack (Vue.js 2) Web Application using Passport](https://www.djamware.com/post/5ac8338780aca714d19d5b9e/securing-mevn-stack-vuejs-2-web-application-using-passport) 4 | 5 | To run locally: 6 | * Run MongoDB server 7 | * Clone this repository 8 | * Run `npm install` 9 | * Run `npm start` 10 | -------------------------------------------------------------------------------- /test/unit/specs/HelloWorld.spec.js: -------------------------------------------------------------------------------- 1 | import Vue from 'vue' 2 | import HelloWorld from '@/components/HelloWorld' 3 | 4 | describe('HelloWorld.vue', () => { 5 | it('should render correct contents', () => { 6 | const Constructor = Vue.extend(HelloWorld) 7 | const vm = new Constructor().$mount() 8 | expect(vm.$el.querySelector('.hello h1').textContent) 9 | .toEqual('Welcome to Your Vue.js App') 10 | }) 11 | }) 12 | -------------------------------------------------------------------------------- /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": [ 3 | ["env", { 4 | "modules": false, 5 | "targets": { 6 | "browsers": ["> 1%", "last 2 versions", "not ie <= 8"] 7 | } 8 | }], 9 | "stage-2" 10 | ], 11 | "plugins": ["transform-vue-jsx", "transform-runtime"], 12 | "env": { 13 | "test": { 14 | "presets": ["env", "stage-2"], 15 | "plugins": ["transform-vue-jsx", "transform-es2015-modules-commonjs", "dynamic-import-node"] 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/App.vue: -------------------------------------------------------------------------------- 1 | 7 | 8 | 13 | 14 | 24 | -------------------------------------------------------------------------------- /src/main.js: -------------------------------------------------------------------------------- 1 | // The Vue build version to load with the `import` command 2 | // (runtime-only or standalone) has been set in webpack.base.conf with an alias. 3 | import Vue from 'vue' 4 | import BootstrapVue from 'bootstrap-vue' 5 | import App from './App' 6 | import router from './router' 7 | import 'bootstrap/dist/css/bootstrap.css' 8 | import 'bootstrap-vue/dist/bootstrap-vue.css' 9 | 10 | Vue.config.productionTip = false 11 | Vue.use(BootstrapVue) 12 | 13 | /* eslint-disable no-new */ 14 | new Vue({ 15 | el: '#app', 16 | router, 17 | components: { App }, 18 | template: '' 19 | }) 20 | -------------------------------------------------------------------------------- /src/router/index.js: -------------------------------------------------------------------------------- 1 | import Vue from 'vue' 2 | import Router from 'vue-router' 3 | import BookList from '@/components/BookList' 4 | import Login from '@/components/Login' 5 | import Register from '@/components/Register' 6 | 7 | Vue.use(Router) 8 | 9 | export default new Router({ 10 | routes: [ 11 | { 12 | path: '/', 13 | name: 'BookList', 14 | component: BookList 15 | }, 16 | { 17 | path: '/login', 18 | name: 'Login', 19 | component: Login 20 | }, 21 | { 22 | path: '/register', 23 | name: 'Register', 24 | component: Register 25 | } 26 | ] 27 | }) 28 | -------------------------------------------------------------------------------- /test/e2e/specs/test.js: -------------------------------------------------------------------------------- 1 | // For authoring Nightwatch tests, see 2 | // http://nightwatchjs.org/guide#usage 3 | 4 | module.exports = { 5 | 'default e2e tests': function (browser) { 6 | // automatically uses dev Server port from /config.index.js 7 | // default: http://localhost:8080 8 | // see nightwatch.conf.js 9 | const devServer = browser.globals.devServerURL 10 | 11 | browser 12 | .url(devServer) 13 | .waitForElementVisible('#app', 5000) 14 | .assert.elementPresent('.hello') 15 | .assert.containsText('h1', 'Welcome to Your Vue.js App') 16 | .assert.elementCount('img', 1) 17 | .end() 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /config/passport.js: -------------------------------------------------------------------------------- 1 | var JwtStrategy = require('passport-jwt').Strategy, 2 | ExtractJwt = require('passport-jwt').ExtractJwt; 3 | 4 | // load up the user model 5 | var User = require('../models/user'); 6 | var settings = require('../config/settings'); // get settings file 7 | 8 | module.exports = function(passport) { 9 | var opts = {}; 10 | opts.jwtFromRequest = ExtractJwt.fromAuthHeaderWithScheme("jwt"); 11 | opts.secretOrKey = settings.secret; 12 | passport.use(new JwtStrategy(opts, function(jwt_payload, done) { 13 | User.findOne({id: jwt_payload.id}, function(err, user) { 14 | if (err) { 15 | return done(err, false); 16 | } 17 | if (user) { 18 | done(null, user); 19 | } else { 20 | done(null, false); 21 | } 22 | }); 23 | })); 24 | }; 25 | -------------------------------------------------------------------------------- /test/unit/jest.conf.js: -------------------------------------------------------------------------------- 1 | const path = require('path') 2 | 3 | module.exports = { 4 | rootDir: path.resolve(__dirname, '../../'), 5 | moduleFileExtensions: [ 6 | 'js', 7 | 'json', 8 | 'vue' 9 | ], 10 | moduleNameMapper: { 11 | '^@/(.*)$': '/src/$1' 12 | }, 13 | transform: { 14 | '^.+\\.js$': '/node_modules/babel-jest', 15 | '.*\\.(vue)$': '/node_modules/vue-jest' 16 | }, 17 | testPathIgnorePatterns: [ 18 | '/test/e2e' 19 | ], 20 | snapshotSerializers: ['/node_modules/jest-serializer-vue'], 21 | setupFiles: ['/test/unit/setup'], 22 | mapCoverage: true, 23 | coverageDirectory: '/test/unit/coverage', 24 | collectCoverageFrom: [ 25 | 'src/**/*.{js,vue}', 26 | '!src/main.js', 27 | '!src/router/index.js', 28 | '!**/node_modules/**' 29 | ] 30 | } 31 | -------------------------------------------------------------------------------- /test/e2e/custom-assertions/elementCount.js: -------------------------------------------------------------------------------- 1 | // A custom Nightwatch assertion. 2 | // The assertion name is the filename. 3 | // Example usage: 4 | // 5 | // browser.assert.elementCount(selector, count) 6 | // 7 | // For more information on custom assertions see: 8 | // http://nightwatchjs.org/guide#writing-custom-assertions 9 | 10 | exports.assertion = function (selector, count) { 11 | this.message = 'Testing if element <' + selector + '> has count: ' + count 12 | this.expected = count 13 | this.pass = function (val) { 14 | return val === this.expected 15 | } 16 | this.value = function (res) { 17 | return res.value 18 | } 19 | this.command = function (cb) { 20 | var self = this 21 | return this.api.execute(function (selector) { 22 | return document.querySelectorAll(selector).length 23 | }, [selector], function (res) { 24 | cb.call(self, res) 25 | }) 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /.eslintrc.js: -------------------------------------------------------------------------------- 1 | // https://eslint.org/docs/user-guide/configuring 2 | 3 | module.exports = { 4 | root: true, 5 | parserOptions: { 6 | parser: 'babel-eslint' 7 | }, 8 | env: { 9 | browser: true, 10 | }, 11 | extends: [ 12 | // https://github.com/vuejs/eslint-plugin-vue#priority-a-essential-error-prevention 13 | // consider switching to `plugin:vue/strongly-recommended` or `plugin:vue/recommended` for stricter rules. 14 | 'plugin:vue/essential', 15 | // https://github.com/standard/standard/blob/master/docs/RULES-en.md 16 | 'standard' 17 | ], 18 | // required to lint *.vue files 19 | plugins: [ 20 | 'vue' 21 | ], 22 | // add your custom rules here 23 | rules: { 24 | // allow async-await 25 | 'generator-star-spacing': 'off', 26 | // allow debugger during development 27 | 'no-debugger': process.env.NODE_ENV === 'production' ? 'error' : 'off' 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Didin Jamaludin 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /test/e2e/nightwatch.conf.js: -------------------------------------------------------------------------------- 1 | require('babel-register') 2 | var config = require('../../config') 3 | 4 | // http://nightwatchjs.org/gettingstarted#settings-file 5 | module.exports = { 6 | src_folders: ['test/e2e/specs'], 7 | output_folder: 'test/e2e/reports', 8 | custom_assertions_path: ['test/e2e/custom-assertions'], 9 | 10 | selenium: { 11 | start_process: true, 12 | server_path: require('selenium-server').path, 13 | host: '127.0.0.1', 14 | port: 4444, 15 | cli_args: { 16 | 'webdriver.chrome.driver': require('chromedriver').path 17 | } 18 | }, 19 | 20 | test_settings: { 21 | default: { 22 | selenium_port: 4444, 23 | selenium_host: 'localhost', 24 | silent: true, 25 | globals: { 26 | devServerURL: 'http://localhost:' + (process.env.PORT || config.dev.port) 27 | } 28 | }, 29 | 30 | chrome: { 31 | desiredCapabilities: { 32 | browserName: 'chrome', 33 | javascriptEnabled: true, 34 | acceptSslCerts: true 35 | } 36 | }, 37 | 38 | firefox: { 39 | desiredCapabilities: { 40 | browserName: 'firefox', 41 | javascriptEnabled: true, 42 | acceptSslCerts: true 43 | } 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /models/User.js: -------------------------------------------------------------------------------- 1 | var mongoose = require('mongoose'); 2 | var Schema = mongoose.Schema; 3 | var bcrypt = require('bcrypt-nodejs'); 4 | 5 | var UserSchema = new Schema({ 6 | username: { 7 | type: String, 8 | unique: true, 9 | required: true 10 | }, 11 | password: { 12 | type: String, 13 | required: true 14 | } 15 | }); 16 | 17 | UserSchema.pre('save', function (next) { 18 | var user = this; 19 | if (this.isModified('password') || this.isNew) { 20 | bcrypt.genSalt(10, function (err, salt) { 21 | if (err) { 22 | return next(err); 23 | } 24 | bcrypt.hash(user.password, salt, null, function (err, hash) { 25 | if (err) { 26 | return next(err); 27 | } 28 | user.password = hash; 29 | next(); 30 | }); 31 | }); 32 | } else { 33 | return next(); 34 | } 35 | }); 36 | 37 | UserSchema.methods.comparePassword = function (passw, cb) { 38 | bcrypt.compare(passw, this.password, function (err, isMatch) { 39 | if (err) { 40 | return cb(err); 41 | } 42 | cb(null, isMatch); 43 | }); 44 | }; 45 | 46 | module.exports = mongoose.model('User', UserSchema); 47 | -------------------------------------------------------------------------------- /app.js: -------------------------------------------------------------------------------- 1 | var express = require('express'); 2 | var path = require('path'); 3 | var favicon = require('serve-favicon'); 4 | var logger = require('morgan'); 5 | var bodyParser = require('body-parser'); 6 | 7 | var book = require('./routes/book'); 8 | var app = express(); 9 | var auth = require('./routes/auth'); 10 | 11 | var mongoose = require('mongoose'); 12 | mongoose.Promise = require('bluebird'); 13 | mongoose.connect('mongodb://localhost/mevn-secure', { promiseLibrary: require('bluebird') }) 14 | .then(() => console.log('connection succesful')) 15 | .catch((err) => console.error(err)); 16 | 17 | app.use(logger('dev')); 18 | app.use(bodyParser.json()); 19 | app.use(bodyParser.urlencoded({'extended':'false'})); 20 | app.use(express.static(path.join(__dirname, 'dist'))); 21 | app.use('/books', express.static(path.join(__dirname, 'dist'))); 22 | app.use('/book', book); 23 | app.use('/api/auth', auth); 24 | 25 | // catch 404 and forward to error handler 26 | app.use(function(req, res, next) { 27 | var err = new Error('Not Found'); 28 | err.status = 404; 29 | next(err); 30 | }); 31 | 32 | // restful api error handler 33 | app.use(function(err, req, res, next) { 34 | console.log(err); 35 | 36 | if (req.app.get('env') !== 'development') { 37 | delete err.stack; 38 | } 39 | 40 | res.status(err.statusCode || 500).json(err); 41 | }); 42 | 43 | module.exports = app; 44 | -------------------------------------------------------------------------------- /test/e2e/runner.js: -------------------------------------------------------------------------------- 1 | // 1. start the dev server using production config 2 | process.env.NODE_ENV = 'testing' 3 | 4 | const webpack = require('webpack') 5 | const DevServer = require('webpack-dev-server') 6 | 7 | const webpackConfig = require('../../build/webpack.prod.conf') 8 | const devConfigPromise = require('../../build/webpack.dev.conf') 9 | 10 | let server 11 | 12 | devConfigPromise.then(devConfig => { 13 | const devServerOptions = devConfig.devServer 14 | const compiler = webpack(webpackConfig) 15 | server = new DevServer(compiler, devServerOptions) 16 | const port = devServerOptions.port 17 | const host = devServerOptions.host 18 | return server.listen(port, host) 19 | }) 20 | .then(() => { 21 | // 2. run the nightwatch test suite against it 22 | // to run in additional browsers: 23 | // 1. add an entry in test/e2e/nightwatch.conf.js under "test_settings" 24 | // 2. add it to the --env flag below 25 | // or override the environment flag, for example: `npm run e2e -- --env chrome,firefox` 26 | // For more information on Nightwatch's config file, see 27 | // http://nightwatchjs.org/guide#settings-file 28 | let opts = process.argv.slice(2) 29 | if (opts.indexOf('--config') === -1) { 30 | opts = opts.concat(['--config', 'test/e2e/nightwatch.conf.js']) 31 | } 32 | if (opts.indexOf('--env') === -1) { 33 | opts = opts.concat(['--env', 'chrome']) 34 | } 35 | 36 | const spawn = require('cross-spawn') 37 | const runner = spawn('./node_modules/.bin/nightwatch', opts, { stdio: 'inherit' }) 38 | 39 | runner.on('exit', function (code) { 40 | server.close() 41 | process.exit(code) 42 | }) 43 | 44 | runner.on('error', function (err) { 45 | server.close() 46 | throw err 47 | }) 48 | }) 49 | -------------------------------------------------------------------------------- /src/components/BookList.vue: -------------------------------------------------------------------------------- 1 | 20 | 21 | 69 | -------------------------------------------------------------------------------- /routes/auth.js: -------------------------------------------------------------------------------- 1 | var mongoose = require('mongoose'); 2 | var passport = require('passport'); 3 | var settings = require('../config/settings'); 4 | require('../config/passport')(passport); 5 | var express = require('express'); 6 | var jwt = require('jsonwebtoken'); 7 | var router = express.Router(); 8 | var User = require("../models/user"); 9 | 10 | router.post('/register', function(req, res) { 11 | if (!req.body.username || !req.body.password) { 12 | res.json({success: false, msg: 'Please pass username and password.'}); 13 | } else { 14 | var newUser = new User({ 15 | username: req.body.username, 16 | password: req.body.password 17 | }); 18 | // save the user 19 | newUser.save(function(err) { 20 | if (err) { 21 | return res.json({success: false, msg: 'Username already exists.'}); 22 | } 23 | res.json({success: true, msg: 'Successful created new user.'}); 24 | }); 25 | } 26 | }); 27 | 28 | router.post('/login', function(req, res) { 29 | User.findOne({ 30 | username: req.body.username 31 | }, function(err, user) { 32 | if (err) throw err; 33 | 34 | if (!user) { 35 | res.status(401).send({success: false, msg: 'Authentication failed. User not found.'}); 36 | } else { 37 | // check if password matches 38 | user.comparePassword(req.body.password, function (err, isMatch) { 39 | if (isMatch && !err) { 40 | // if user is found and password is right create a token 41 | var token = jwt.sign(user.toJSON(), settings.secret); 42 | // return the information including token as JSON 43 | res.json({success: true, token: 'JWT ' + token}); 44 | } else { 45 | res.status(401).send({success: false, msg: 'Authentication failed. Wrong password.'}); 46 | } 47 | }); 48 | } 49 | }); 50 | }); 51 | 52 | module.exports = router; 53 | -------------------------------------------------------------------------------- /src/components/Register.vue: -------------------------------------------------------------------------------- 1 | 31 | 32 | 62 | -------------------------------------------------------------------------------- /bin/www: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | /** 4 | * Module dependencies. 5 | */ 6 | 7 | var app = require('../app'); 8 | var debug = require('debug')('mean-app:server'); 9 | var http = require('http'); 10 | 11 | /** 12 | * Get port from environment and store in Express. 13 | */ 14 | 15 | var port = normalizePort(process.env.PORT || '3000'); 16 | app.set('port', port); 17 | 18 | /** 19 | * Create HTTP server. 20 | */ 21 | 22 | var server = http.createServer(app); 23 | 24 | /** 25 | * Listen on provided port, on all network interfaces. 26 | */ 27 | 28 | server.listen(port); 29 | server.on('error', onError); 30 | server.on('listening', onListening); 31 | 32 | /** 33 | * Normalize a port into a number, string, or false. 34 | */ 35 | 36 | function normalizePort(val) { 37 | var port = parseInt(val, 10); 38 | 39 | if (isNaN(port)) { 40 | // named pipe 41 | return val; 42 | } 43 | 44 | if (port >= 0) { 45 | // port number 46 | return port; 47 | } 48 | 49 | return false; 50 | } 51 | 52 | /** 53 | * Event listener for HTTP server "error" event. 54 | */ 55 | 56 | function onError(error) { 57 | if (error.syscall !== 'listen') { 58 | throw error; 59 | } 60 | 61 | var bind = typeof port === 'string' 62 | ? 'Pipe ' + port 63 | : 'Port ' + port; 64 | 65 | // handle specific listen errors with friendly messages 66 | switch (error.code) { 67 | case 'EACCES': 68 | console.error(bind + ' requires elevated privileges'); 69 | process.exit(1); 70 | break; 71 | case 'EADDRINUSE': 72 | console.error(bind + ' is already in use'); 73 | process.exit(1); 74 | break; 75 | default: 76 | throw error; 77 | } 78 | } 79 | 80 | /** 81 | * Event listener for HTTP server "listening" event. 82 | */ 83 | 84 | function onListening() { 85 | var addr = server.address(); 86 | var bind = typeof addr === 'string' 87 | ? 'pipe ' + addr 88 | : 'port ' + addr.port; 89 | debug('Listening on ' + bind); 90 | } 91 | -------------------------------------------------------------------------------- /src/components/Login.vue: -------------------------------------------------------------------------------- 1 | 31 | 32 | 67 | -------------------------------------------------------------------------------- /routes/book.js: -------------------------------------------------------------------------------- 1 | var express = require('express'); 2 | var router = express.Router(); 3 | var mongoose = require('mongoose'); 4 | var Book = require('../models/Book.js'); 5 | var passport = require('passport'); 6 | require('../config/passport')(passport); 7 | 8 | /* GET ALL BOOKS */ 9 | router.get('/', passport.authenticate('jwt', { session: false}), function(req, res) { 10 | var token = getToken(req.headers); 11 | if (token) { 12 | Book.find(function (err, books) { 13 | if (err) return next(err); 14 | res.json(books); 15 | }); 16 | } else { 17 | return res.status(403).send({success: false, msg: 'Unauthorized.'}); 18 | } 19 | }); 20 | 21 | /* GET SINGLE BOOK BY ID */ 22 | router.get('/:id', function(req, res, next) { 23 | Book.findById(req.params.id, function (err, post) { 24 | if (err) return next(err); 25 | res.json(post); 26 | }); 27 | }); 28 | 29 | /* SAVE BOOK */ 30 | router.post('/', passport.authenticate('jwt', { session: false}), function(req, res) { 31 | var token = getToken(req.headers); 32 | if (token) { 33 | Book.create(req.body, function (err, post) { 34 | if (err) return next(err); 35 | res.json(post); 36 | }); 37 | } else { 38 | return res.status(403).send({success: false, msg: 'Unauthorized.'}); 39 | } 40 | }); 41 | 42 | /* UPDATE BOOK */ 43 | router.put('/:id', function(req, res, next) { 44 | Book.findByIdAndUpdate(req.params.id, req.body, function (err, post) { 45 | if (err) return next(err); 46 | res.json(post); 47 | }); 48 | }); 49 | 50 | /* DELETE BOOK */ 51 | router.delete('/:id', function(req, res, next) { 52 | Book.findByIdAndRemove(req.params.id, req.body, function (err, post) { 53 | if (err) return next(err); 54 | res.json(post); 55 | }); 56 | }); 57 | 58 | getToken = function (headers) { 59 | if (headers && headers.authorization) { 60 | var parted = headers.authorization.split(' '); 61 | if (parted.length === 2) { 62 | return parted[1]; 63 | } else { 64 | return null; 65 | } 66 | } else { 67 | return null; 68 | } 69 | }; 70 | 71 | module.exports = router; 72 | -------------------------------------------------------------------------------- /src/components/HelloWorld.vue: -------------------------------------------------------------------------------- 1 | 85 | 86 | 96 | 97 | 98 | 114 | -------------------------------------------------------------------------------- /config/index.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | // Template version: 1.3.1 3 | // see http://vuejs-templates.github.io/webpack for documentation. 4 | 5 | const path = require('path') 6 | 7 | module.exports = { 8 | dev: { 9 | 10 | // Paths 11 | assetsSubDirectory: 'static', 12 | assetsPublicPath: '/', 13 | proxyTable: {}, 14 | 15 | // Various Dev Server settings 16 | host: 'localhost', // can be overwritten by process.env.HOST 17 | port: 8080, // can be overwritten by process.env.PORT, if port is in use, a free one will be determined 18 | autoOpenBrowser: false, 19 | errorOverlay: true, 20 | notifyOnErrors: true, 21 | poll: false, // https://webpack.js.org/configuration/dev-server/#devserver-watchoptions- 22 | 23 | // Use Eslint Loader? 24 | // If true, your code will be linted during bundling and 25 | // linting errors and warnings will be shown in the console. 26 | useEslint: true, 27 | // If true, eslint errors and warnings will also be shown in the error overlay 28 | // in the browser. 29 | showEslintErrorsInOverlay: false, 30 | 31 | /** 32 | * Source Maps 33 | */ 34 | 35 | // https://webpack.js.org/configuration/devtool/#development 36 | devtool: 'cheap-module-eval-source-map', 37 | 38 | // If you have problems debugging vue-files in devtools, 39 | // set this to false - it *may* help 40 | // https://vue-loader.vuejs.org/en/options.html#cachebusting 41 | cacheBusting: true, 42 | 43 | cssSourceMap: true 44 | }, 45 | 46 | build: { 47 | // Template for index.html 48 | index: path.resolve(__dirname, '../dist/index.html'), 49 | 50 | // Paths 51 | assetsRoot: path.resolve(__dirname, '../dist'), 52 | assetsSubDirectory: 'static', 53 | assetsPublicPath: '/', 54 | 55 | /** 56 | * Source Maps 57 | */ 58 | 59 | productionSourceMap: true, 60 | // https://webpack.js.org/configuration/devtool/#production 61 | devtool: '#source-map', 62 | 63 | // Gzip off by default as many popular static hosts such as 64 | // Surge or Netlify already gzip all static assets for you. 65 | // Before setting to `true`, make sure to: 66 | // npm install --save-dev compression-webpack-plugin 67 | productionGzip: false, 68 | productionGzipExtensions: ['js', 'css'], 69 | 70 | // Run the build command with an extra argument to 71 | // View the bundle analyzer report after build finishes: 72 | // `npm run build --report` 73 | // Set to `true` or `false` to always turn it on or off 74 | bundleAnalyzerReport: process.env.npm_config_report 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "mevn-secure", 3 | "version": "1.0.0", 4 | "description": "A Vue.js project", 5 | "author": "didin jamaludin ", 6 | "private": true, 7 | "scripts": { 8 | "dev": "webpack-dev-server --inline --progress --config build/webpack.dev.conf.js", 9 | "start": "npm run build && node ./bin/www", 10 | "unit": "jest --config test/unit/jest.conf.js --coverage", 11 | "e2e": "node test/e2e/runner.js", 12 | "test": "npm run unit && npm run e2e", 13 | "lint": "eslint --ext .js,.vue src test/unit test/e2e/specs", 14 | "build": "node build/build.js" 15 | }, 16 | "dependencies": { 17 | "axios": "^0.18.0", 18 | "bcrypt-nodejs": "0.0.3", 19 | "bluebird": "^3.5.1", 20 | "body-parser": "^1.18.2", 21 | "bootstrap": "^4.0.0-beta.2", 22 | "bootstrap-vue": "^2.0.0-rc.6", 23 | "express": "^4.16.3", 24 | "jsonwebtoken": "^8.2.0", 25 | "mongoose": "^5.0.12", 26 | "morgan": "^1.9.0", 27 | "passport": "^0.4.0", 28 | "passport-jwt": "^4.0.0", 29 | "serve-favicon": "^2.5.0", 30 | "vue": "^2.5.2", 31 | "vue-router": "^3.0.1" 32 | }, 33 | "devDependencies": { 34 | "autoprefixer": "^7.1.2", 35 | "babel-core": "^6.22.1", 36 | "babel-eslint": "^8.2.1", 37 | "babel-helper-vue-jsx-merge-props": "^2.0.3", 38 | "babel-jest": "^21.0.2", 39 | "babel-loader": "^7.1.1", 40 | "babel-plugin-dynamic-import-node": "^1.2.0", 41 | "babel-plugin-syntax-jsx": "^6.18.0", 42 | "babel-plugin-transform-es2015-modules-commonjs": "^6.26.0", 43 | "babel-plugin-transform-runtime": "^6.22.0", 44 | "babel-plugin-transform-vue-jsx": "^3.5.0", 45 | "babel-preset-env": "^1.3.2", 46 | "babel-preset-stage-2": "^6.22.0", 47 | "babel-register": "^6.22.0", 48 | "chalk": "^2.0.1", 49 | "chromedriver": "^2.27.2", 50 | "copy-webpack-plugin": "^4.0.1", 51 | "cross-spawn": "^5.0.1", 52 | "css-loader": "^0.28.0", 53 | "eslint": "^4.15.0", 54 | "eslint-config-standard": "^10.2.1", 55 | "eslint-friendly-formatter": "^3.0.0", 56 | "eslint-loader": "^1.7.1", 57 | "eslint-plugin-import": "^2.7.0", 58 | "eslint-plugin-node": "^5.2.0", 59 | "eslint-plugin-promise": "^3.4.0", 60 | "eslint-plugin-standard": "^3.0.1", 61 | "eslint-plugin-vue": "^4.0.0", 62 | "extract-text-webpack-plugin": "^3.0.0", 63 | "file-loader": "^1.1.4", 64 | "friendly-errors-webpack-plugin": "^1.6.1", 65 | "html-webpack-plugin": "^2.30.1", 66 | "jest": "^22.0.4", 67 | "jest-serializer-vue": "^0.3.0", 68 | "nightwatch": "^0.9.12", 69 | "node-notifier": "^5.1.2", 70 | "optimize-css-assets-webpack-plugin": "^3.2.0", 71 | "ora": "^1.2.0", 72 | "portfinder": "^1.0.13", 73 | "postcss-import": "^11.0.0", 74 | "postcss-loader": "^2.0.8", 75 | "postcss-url": "^7.2.1", 76 | "rimraf": "^2.6.0", 77 | "selenium-server": "^3.0.1", 78 | "semver": "^5.3.0", 79 | "shelljs": "^0.7.6", 80 | "uglifyjs-webpack-plugin": "^1.1.1", 81 | "url-loader": "^0.5.8", 82 | "vue-jest": "^1.0.2", 83 | "vue-loader": "^13.3.0", 84 | "vue-style-loader": "^3.0.1", 85 | "vue-template-compiler": "^2.5.2", 86 | "webpack": "^3.6.0", 87 | "webpack-bundle-analyzer": "^2.9.0", 88 | "webpack-dev-server": "^2.9.1", 89 | "webpack-merge": "^4.1.0" 90 | }, 91 | "engines": { 92 | "node": ">= 6.0.0", 93 | "npm": ">= 3.0.0" 94 | }, 95 | "browserslist": [ 96 | "> 1%", 97 | "last 2 versions", 98 | "not ie <= 8" 99 | ] 100 | } 101 | --------------------------------------------------------------------------------