|
| 1 | +module.exports = function(app, passport, SERVER_SECRET) { |
| 2 | + |
| 3 | + // default message |
| 4 | + app.get('/', function (req, res) { |
| 5 | + res.send('<html><body><p>Welcome to the database</p></body></html>'); |
| 6 | + }); |
| 7 | + |
| 8 | +// =========== authenticate login info and generate access token =============== |
| 9 | + |
| 10 | + app.post('/login', function(req, res, next) { |
| 11 | + passport.authenticate('local-login', function(err, user, info) { |
| 12 | + if (err) { return next(err); } |
| 13 | + // stop if it fails |
| 14 | + if (!user) { return res.json({ message: 'Invalid Username of Password' }); } |
| 15 | + |
| 16 | + req.logIn(user, function(err) { |
| 17 | + // return if does not match |
| 18 | + if (err) { return next(err); } |
| 19 | + |
| 20 | + // generate token if it succeeds |
| 21 | + const db = { |
| 22 | + updateOrCreate: function(user, cb){ |
| 23 | + cb(null, user); |
| 24 | + } |
| 25 | + }; |
| 26 | + db.updateOrCreate(req.user, function(err, user){ |
| 27 | + if(err) {return next(err);} |
| 28 | + // store the updated information in req.user again |
| 29 | + req.user = { |
| 30 | + id: user.username |
| 31 | + }; |
| 32 | + }); |
| 33 | + |
| 34 | + // create token |
| 35 | + const jwt = require('jsonwebtoken'); |
| 36 | + req.token = jwt.sign({ |
| 37 | + id: req.user.id, |
| 38 | + }, SERVER_SECRET); |
| 39 | + |
| 40 | + // lastly respond with json |
| 41 | + return res.status(200).json({ |
| 42 | + user: req.user, |
| 43 | + token: req.token |
| 44 | + }); |
| 45 | + }); |
| 46 | + })(req, res, next); |
| 47 | + }); |
| 48 | + |
| 49 | +// ============================================================================= |
| 50 | + |
| 51 | +// ==================== Allows users to create accounts ======================== |
| 52 | + |
| 53 | + app.post('/signup', passport.authenticate('local-signup', { |
| 54 | + successRedirect : '/signup/successjson', |
| 55 | + failureRedirect : '/signup/failurejson', |
| 56 | + failureFlash : true |
| 57 | + })); |
| 58 | + // return messages for signup users |
| 59 | + app.get('/signup/successjson', function(req, res) { |
| 60 | + res.json({ message: 'Successfully created user' }); |
| 61 | + }); |
| 62 | + |
| 63 | + app.get('/signup/failurejson', function(req, res) { |
| 64 | + res.json({ message: 'This user already exists' }); |
| 65 | + }); |
| 66 | + |
| 67 | +// ============================================================================= |
| 68 | + |
| 69 | +// ================= Protected APIs for authenticated Users ==================== |
| 70 | + |
| 71 | + // get tools and routes |
| 72 | + var expressJwt = require('express-jwt'), |
| 73 | + REST_POST = require('../routes/REST_POST'), |
| 74 | + REST_GET = require('../routes/REST_GET'), |
| 75 | + REST_EDIT = require('../routes/REST_EDIT'), |
| 76 | + REST_DELETE = require('../routes/REST_DELETE'); |
| 77 | + |
| 78 | + // authenticate access token |
| 79 | + const authenticate = expressJwt({secret : SERVER_SECRET}); |
| 80 | + |
| 81 | + // GET, EndPoint: |
| 82 | + // https://127.0.0.1:5000/product/api/all?order={orderby} |
| 83 | + app.get('/product/api/get/all', authenticate, REST_GET.getAllRecords); |
| 84 | + |
| 85 | + // GET, Endpoint: |
| 86 | + // https://127.0.0.1:5000/product/api/?c={target_column}&q={target_value}&order={orderby} |
| 87 | + app.get('/product/api/get', authenticate, REST_GET.findByColumn); |
| 88 | + |
| 89 | + // GET, EndPoint: |
| 90 | + // https://127.0.0.1:5000/product/api/search/?c={target_column}&start={start}&end={end}&order={orderby} |
| 91 | + app.get('/product/api/get/search', authenticate, REST_GET.rangeSearch); |
| 92 | + |
| 93 | + // POST, Endpoint: |
| 94 | + // https://127.0.0.1:5000/product/api/add/?content=1,2,3... |
| 95 | + app.post('/product/api/add', authenticate, REST_POST.addOne); |
| 96 | + |
| 97 | + // POST, Endpoint: |
| 98 | + // https://127.0.0.1:5000/product/api/add/?content[0]=1,2,3,...&content[1]=1,2,3... |
| 99 | + app.post('/product/api/add/batch/', authenticate, REST_POST.addBatch); |
| 100 | + |
| 101 | + // EDIT, Endpoint: |
| 102 | + // https://127.0.0.1:5000/product/api/edit/:orderID/?content={} |
| 103 | + app.post('/product/api/edit/:id', authenticate, REST_EDIT); |
| 104 | + |
| 105 | + // Endpoint: https://127.0.0.1:5000/product/api/delete/?id={orderID} |
| 106 | + app.delete('/product/api/delete/', authenticate, REST_DELETE); |
| 107 | + |
| 108 | +// ============================================================================= |
| 109 | + |
| 110 | +} |
0 commit comments