Skip to content

Commit

Permalink
clean up and commenting
Browse files Browse the repository at this point in the history
  • Loading branch information
Hemil96 committed Aug 13, 2018
1 parent da31b96 commit c4db724
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 20 deletions.
8 changes: 8 additions & 0 deletions methods/actions.js → controller/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const Book = require('../model/book');
const config = require('../config/database');
const utils = require('../helper/utils');

// POST /authenticate
const authenticate = (req, res) => {
User.findOne({ name: req.body.name })
.then((user) => {
Expand All @@ -22,6 +23,8 @@ const authenticate = (req, res) => {
});
};


// POST /adduser
const addNew = (req, res) => {
if ((!req.body.name) || (!req.body.password)) {
return res.status(400).json({ success: false, msg: 'Enter all values' });
Expand All @@ -43,6 +46,8 @@ const addNew = (req, res) => {
});
};


// GET /getinfo
const getinfo = (req, res) => {
if (req.headers.authorization && req.headers.authorization.split(' ')[0] === 'Bearer') {
const token = req.headers.authorization.split(' ')[1];
Expand All @@ -52,6 +57,8 @@ const getinfo = (req, res) => {
return res.status(400).json({ success: false, msg: 'No header' });
};


// POST /addBook
const addBook = (req, res) => {
const bookToCreate = {
name: req.body.name,
Expand All @@ -70,6 +77,7 @@ const addBook = (req, res) => {
});
};

// GET /getBooks
const getBook = (req, res) => {
Book.find({ userId: req.user_id })
.then((foundBook) => {
Expand Down
2 changes: 2 additions & 0 deletions methods/auth.js → controller/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ passport.use(new JwtBearerStrategy(
},
));


// Middlewares
exports.isBearerAuthenticated = passport.authenticate('jwt-bearer', { session: false });

exports.isAuthenticated = passport.authenticate(['jwt'], { session: false });
Expand Down
8 changes: 4 additions & 4 deletions methods/client.js → controller/client.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Load required packages
var Client = require('../model/client');

// /client for POST
// POST /postClients
const postClients = (req, res) => {
// Set the client properties that came from the POST data
const clientToCreate = {
Expand All @@ -26,7 +26,7 @@ const postClients = (req, res) => {
});
};

// Create endpoint /api/clients for GET
// GET /getClients
const getClients = (req, res) => {
// Use the Client model to find all clients
Client.find({ userId: req.query.userId })
Expand All @@ -40,9 +40,9 @@ const getClients = (req, res) => {
});
};

const clientMethods = {
const clientCtr = {
postClients,
getClients,
};

module.exports = clientMethods;
module.exports = clientCtr;
File renamed without changes.
2 changes: 1 addition & 1 deletion helper/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const bcrypt = require('bcrypt');
const User = require('../model/user');

// Comapre text password and hash
const comparePassword = (textPassword, hash) => {
return new Promise((resolve, reject) => {
bcrypt.compare(textPassword, hash)
Expand Down
28 changes: 14 additions & 14 deletions routes/routes.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
const express = require('express');
const actions = require('../methods/actions');
const auth = require('../methods/auth');
const client = require('../methods/client');
const oauth2 = require('../methods/oauth2');
const actions = require('../controller/actions');
const auth = require('../controller/auth');
const client = require('../controller/client');
const oauth2 = require('../controller/oauth2');

const router = express.Router();

// User
router.post('/authenticate', actions.authenticate);
router.post('/adduser', actions.addNew);
router.get('/getinfo', actions.getinfo);
router.post('/authenticate', actions.authenticate); // Authenticate user
router.post('/adduser', actions.addNew); // Create New User
router.get('/getinfo', actions.getinfo); // Retrive All Users

// Client routes
router.post('/clients', auth.isAuthenticated, client.postClients);
router.get('/clients', auth.isAuthenticated, client.getClients);
router.post('/clients', auth.isAuthenticated, client.postClients); // Create New User
router.get('/clients', auth.isAuthenticated, client.getClients); // Authenticate client

// Oauth routes
router.get('/oauth2/authorize', oauth2.authorization);
router.post('/oauth2/authorize', oauth2.decision);
router.get('/oauth2/authorize', oauth2.authorization); // Ask for permission
router.post('/oauth2/authorize', oauth2.decision); // Retrive the code from url

// Exchange code with token
router.post('/oauth2/token', auth.isClientAuthenticated, oauth2.token);
router.post('/oauth2/token', auth.isClientAuthenticated, oauth2.token); // Generate token

// Accessing/modifying the resourse
router.post('/addBook', auth.isBearerAuthenticated, actions.addBook);
router.get('/getBooks', auth.isBearerAuthenticated, actions.getBook);
router.post('/addBook', auth.isBearerAuthenticated, actions.addBook); // Add books
router.get('/getBooks', auth.isBearerAuthenticated, actions.getBook); // Get Books

module.exports = router;
2 changes: 1 addition & 1 deletion server.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const passport = require('passport');
const bodyParser = require('body-parser');
const session = require('express-session');
const ejs = require('ejs');
const auth = require('./methods/auth.js');
const auth = require('./controller/auth.js');
const routes = require('./routes/routes');
const config = require('./config/database');

Expand Down

0 comments on commit c4db724

Please sign in to comment.