<repo_name>1000pilar/final-live-code<file_sep>/server/controllers/articles.js
var Article = require('../models/article.js')


module.exports = {
  create: (req, res)=>{
    var createActicle = new Article({
      title: req.body.title,
      content: req.body.content,
      category: req.body.category,
      author: req.body.author_id
    })
    createActicle.save((err, data)=>{
      if(err){
        res.send(err)
      } else {
        res.send(data)
      }
    })
  },
  findAll: (req, res)=>{
    Article.find((err, result)=>{
      if (err) {
        res.send(err)
      } else {
        res.send(result)
      }
    })
  },
  findCategory: (req, res)=>{
    Article.find({category: req.params.category}, (err, result)=>{
      if(!err) {
        res.send(result)
      } else {
        res.send(err)
      }
    })
  },
  update: (req, res)=>{
    Article.findOneAndUpdate({_id: req.params.id}, {$set:{
      title: req.body.title,
      content: req.body.content,
      category: req.body.category,
      author: req.body.author_id
    }}, {new: true}, (err, data)=>{
      if (err) {
        res.send(err)
      } else {
        res.send(data)
      }
    })
  },
  delete: (req, res)=>{
    Article.remove({_id: req.params.id}, (err, data)=>{
      if (err) {
        res.send(err)
      } else {
        res.send(data)
      }
    })
  }
}
<file_sep>/server/routes/articles.js
var express = require('express')
var router = express.Router()
var articleController = require('../controllers/articles.js')
var helper = require('../helpers/jwtVerify.js')


router.post('/', helper.jwtVerify, articleController.create)
router.get('/', articleController.findAll)
router.get('/:category', articleController.findCategory)
router.put('/:id', helper.jwtVerify, articleController.update)
router.delete('/:id', helper.jwtVerify, articleController.delete)


module.exports = router
<file_sep>/README.md
# final-live-code

## install npm on server-side
"bcrypt": "^1.0.2",
"body-parser": "^1.17.2",
"cors": "^2.8.3",
"express": "^4.15.3",
"jsonwebtoken": "^7.4.1",
"mongoose": "^4.10.6",
"passport": "^0.3.2",
"passport-local": "^1.0.0"
<file_sep>/server/controllers/users.js
var User = require('../models/user.js')
var jwt = require('jsonwebtoken')
var bcrypt = require('bcrypt')


module.exports = {
  signup : (req, res)=>{

    var userCreate = new User({
      name: req.body.name,
      username: req.body.username,
      password: <PASSWORD>.hashSync(req.body.password, 10),
      email: req.body.email,
      role: req.body.role || 'member'
    })
    userCreate.save((err, user)=>{
      if(err){
        res.send(err)
      } else {
        res.send(user)
      }
    })
  },
  signin: (req, res)=>{
    var user = req.user
    console.log(user);
    if(user.hasOwnProperty('message')){
      res.send(req.user)
    } else {
      var token = jwt.sign({
        username: user.username,
        role: user.role
      }, 'rahasia', (err, token)=>{
        res.send(token)
      })
    }
  },
  getAll: (req, res)=>{
    User.find((err, result)=>{
      if(!err){
        res.send(result)
      } else {
        res.send(err)
      }
    })
  }
}
<file_sep>/server/helpers/jwtVerify.js
var express = require('express')
var jwt = require('jsonwebtoken')


module.exports = {
  jwtVerify : function (req, res, next) {
    jwt.verify(req.headers.token, '<PASSWORD>', function(err, decoded) {
      if (decoded.role == 'admin') {
        req.decoded = decoded
        next()
      } else {
        res.send({message: `only admin could create,modify,delete article`})
      }
    });
  }
}
<file_sep>/server/app.js
const express = require('express')
const passport = require('passport')
const LocalStrategy = require('passport-local')
const bcrypt = require('bcrypt')
const bodyParser = require('body-parser')
const app = express()
const User = require('./models/user.js')


var users = require('./routes/users.js')
var articles = require('./routes/articles.js')

const mongoose = require('mongoose')
mongoose.connect("mongodb://localhost:live-code")
console.log(`Connect To database`);

passport.use(new LocalStrategy(
  function(username, password, done) {
    User.findOne({ username: username }, function (err, user) {
      console.log(password);
      console.log(user.password);
      console.log(username);
      if (err) { return done(err); }
      if (!user) { return done(null, {message: `username or password wrong`}); }
      if (!bcrypt.compareSync(password, user.password)) { return done(null, {message: `username or password wrong`}); }
      return done(null, user);
    });
  }
));



app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended:false}))

app.use('/api/users', users)
app.use('/api/articles', articles)

app.listen(3000)
console.log(`Connect to port 3000`);
<file_sep>/client/src/store.js
import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'

Vue.use(Vuex)

export const store = new Vuex.store({
  state:{
    articles: []
  },
  getters: {
    getUser (state) {
      return state.user
    },
    getArticles (state) {
      return state.articles
    }
  },
  mutations: {
    
  }
})
