Browse Source

Updated admin routes to support the login.

pull/61/head
jrtechs 5 years ago
parent
commit
c7ffc3a8cb
13 changed files with 170 additions and 52 deletions
  1. +17
    -3
      admin/adminDownloads.js
  2. +19
    -5
      admin/adminHome.js
  3. +17
    -1
      admin/posts.js
  4. +20
    -1
      admin/users.js
  5. +1
    -1
      includes/includes.js
  6. +3
    -1
      routes/about.js
  7. +9
    -9
      routes/admin/index.js
  8. +8
    -0
      routes/admin/login.js
  9. +3
    -1
      routes/contact.js
  10. +8
    -0
      routes/index.js
  11. +18
    -29
      sites/admin.js
  12. +1
    -1
      templates/admin/adminMain.html
  13. +46
    -0
      utils/utils.js

+ 17
- 3
admin/adminDownloads.js View File

@ -109,13 +109,11 @@ module.exports=
* @param templateContext json object used as the template context * @param templateContext json object used as the template context
* @returns {Promise} renders the template used for this page * @returns {Promise} renders the template used for this page
*/ */
main: function(postData, templateContext)
main: function(templateContext)
{ {
return new Promise(function(resolve, reject) return new Promise(function(resolve, reject)
{ {
Promise.all([includes.fetchTemplate(TEMPLATE_FILE), Promise.all([includes.fetchTemplate(TEMPLATE_FILE),
addDownloadPostData(postData),
removeDownloads(postData),
displayDownloads(templateContext)]).then(function(template) displayDownloads(templateContext)]).then(function(template)
{ {
resolve(template[0]); resolve(template[0]);
@ -125,5 +123,21 @@ module.exports=
reject(error); reject(error);
}); });
}); });
},
processPostData: function(postData, templateContext)
{
return new Promise(function(resolve, reject)
{
Promise.all([addDownloadPostData(postData),
removeDownloads(postData)]).then(function()
{
resolve();
}).catch(function(error)
{
console.log(error);
reject(error);
})
})
} }
}; };

+ 19
- 5
admin/adminHome.js View File

@ -116,15 +116,12 @@ module.exports=
* @param templateContext json object used as the template context * @param templateContext json object used as the template context
* @returns {Promise} renders the template used for this page * @returns {Promise} renders the template used for this page
*/ */
main: function(postData, templateContext)
main: function(templateContext)
{ {
console.log("called");
return new Promise(function(resolve, reject) return new Promise(function(resolve, reject)
{ {
Promise.all([includes.fetchTemplate(TEMPLATE_FILE), Promise.all([includes.fetchTemplate(TEMPLATE_FILE),
processPostAddCategory(postData),
appendCategoriesToTemplate(templateContext),
processPost(postData)])
appendCategoriesToTemplate(templateContext)])
.then(function(template) .then(function(template)
{ {
resolve(template[0]); resolve(template[0]);
@ -134,5 +131,22 @@ module.exports=
reject(error); reject(error);
}); });
}); });
},
processPostData: function(postData, templateContexgt)
{
return new Promise(function(resolve, reject)
{
Promise.all([processPostAddCategory(postData),
processPost(postData)])
.then(function(data)
{
resolve();
}).catch(function(error)
{
console.log("error in add downloads.js");
reject(error);
});
});
} }
}; };

+ 17
- 1
admin/posts.js View File

@ -84,7 +84,23 @@ module.exports=
* @param templateContext json object used as the template context * @param templateContext json object used as the template context
* @returns {Promise} renders the template used for this page * @returns {Promise} renders the template used for this page
*/ */
main: function(postData, templateContext)
main: function(templateContext)
{
return new Promise(function(resolve, reject)
{
Promise.all([includes.fetchTemplate(TEMPLATE_FILE),
fetchPostsInformation(templateContext)]).then(function(template)
{
resolve(template[0]);
}).catch(function(error)
{
console.log("error in add admin blog.js");
reject(error);
});
});
},
processPostData(postData, templateContext)
{ {
return new Promise(function(resolve, reject) return new Promise(function(resolve, reject)
{ {

+ 20
- 1
admin/users.js View File

@ -167,7 +167,26 @@ module.exports=
resolve(template[0]); resolve(template[0]);
}).catch(function(error) }).catch(function(error)
{ {
console.log("error in add downloads.js");
console.log("error in users.js");
reject(error);
});
});
},
processPostData: function(postData, templateContext)
{
return new Promise(function(resolve, reject)
{
Promise.all([includes.fetchTemplate(TEMPLATE_FILE),
addUserPostData(postData),
removeUserPost(postData),
editUserPost(postData, templateContext),
getUserInformation(templateContext)]).then(function(template)
{
resolve(template[0]);
}).catch(function(error)
{
console.log("error in users.js");
reject(error); reject(error);
}); });
}); });

+ 1
- 1
includes/includes.js View File

@ -153,7 +153,7 @@ module.exports =
*/ */
printAdminHeader(templateContext) printAdminHeader(templateContext)
{ {
return utils.includeInObject(HEADER_KEY, templateContext, ADMIN_HEADER);
return module.exports.includeInObject(HEADER_KEY, templateContext, ADMIN_HEADER);
}, },

+ 3
- 1
routes/about.js View File

@ -1,5 +1,7 @@
const routes = require('express').Router(); const routes = require('express').Router();
const utils = require('../utils/utils.js');
routes.get('/', (request, result) => routes.get('/', (request, result) =>
{ {
@ -8,7 +10,7 @@ routes.get('/', (request, result) =>
routes.get('*', (request, result) => routes.get('*', (request, result) =>
{ {
//error
utils.print404(result);
}); });
module.exports = routes; module.exports = routes;

+ 9
- 9
routes/admin/index.js View File

@ -15,21 +15,21 @@ routes.use('/downloads', downloads);
const users = require('./users'); const users = require('./users');
routes.use('/users', users); routes.use('/users', users);
const utils = require('../../utils/utils.js');
routes.get('/', (request, result) => routes.get('/', (request, result) =>
{ {
if(utils.checkPrivilege(request) >= utils.PRIVILEGE.MEMBER)
{
utils.renderHTML(request, result, "users.html", getUserInformation);
}
else
{
utils.printError(result, "You need to be logged in");
}
utils.constructAdminPage(request, result, require("../../admin/adminHome").main)
});
routes.post('/', (request, result) =>
{
utils.constructAdminPage(request, result, require("../../admin/adminHome").processPostData);
}); });
routes.get('*', (request, result) => routes.get('*', (request, result) =>
{ {
//error
utils.print404(result);
}); });
module.exports = routes; module.exports = routes;

+ 8
- 0
routes/admin/login.js View File

@ -1,8 +1,16 @@
const routes = require('express').Router(); const routes = require('express').Router();
const utils = require('../../utils/utils.js');
routes.get('/', (request, result) => routes.get('/', (request, result) =>
{ {
utils.constructAdminPage(request, result, require("../../admin/adminHome").main)
});
routes.post('/', (request, result) =>
{
utils.constructAdminPage(request, result, require("../../admin/login").main)
}); });
module.exports = routes; module.exports = routes;

+ 3
- 1
routes/contact.js View File

@ -1,5 +1,7 @@
const routes = require('express').Router(); const routes = require('express').Router();
const utils = require('../utils/utils.js');
routes.get('/', (request, result) => routes.get('/', (request, result) =>
{ {
require("../blog/contact.js").main(request, result); require("../blog/contact.js").main(request, result);
@ -7,7 +9,7 @@ routes.get('/', (request, result) =>
routes.get('*', (request, result) => routes.get('*', (request, result) =>
{ {
//error
utils.print404(result);
}); });
module.exports = routes; module.exports = routes;

+ 8
- 0
routes/index.js View File

@ -1,14 +1,22 @@
const routes = require('express').Router(); const routes = require('express').Router();
/** about page */
const about = require('./about'); const about = require('./about');
routes.use('/about', about); routes.use('/about', about);
/** admin page and all of its sub pages */
const admin = require('./admin');
routes.use('/admin', admin);
/** Contact page */
const contact = require('./contact'); const contact = require('./contact');
routes.use('/contact', contact); routes.use('/contact', contact);
/** Downloads path for files */
const downloads = require('./downloads'); const downloads = require('./downloads');
routes.use('/downloads', downloads); routes.use('/downloads', downloads);
/** static content like css, js, and images */
const includes = require('./includes'); const includes = require('./includes');
routes.use('/includes', includes); routes.use('/includes', includes);

+ 18
- 29
sites/admin.js View File

@ -1,9 +1,6 @@
//sending static content //sending static content
const includes = require('../includes/includes.js'); const includes = require('../includes/includes.js');
//used to append static content to result
const contentLoader = require('../includes/staticContentServer.js');
const whiskers = require('whiskers'); const whiskers = require('whiskers');
@ -22,34 +19,26 @@ module.exports=
*/ */
main: function(request, result, filename) main: function(request, result, filename)
{ {
if(contentLoader.serveStaticContent(request, result, filename, ""))
{
//do nothing
}
else
{
const clientAddress = (request.headers['x-forwarded-for'] || '').split(',')[0]
|| request.connection.remoteAddress;
const clientAddress = (request.headers['x-forwarded-for'] || '').split(',')[0]
|| request.connection.remoteAddress;
result.writeHead(200, {'Content-Type': 'text/html'});
result.writeHead(200, {'Content-Type': 'text/html'});
const file = "../admin/admin.js";
const file = "../admin/admin.js";
var templateContext = Object();
Promise.all([includes.fetchTemplate("admin/adminMain.html"),
includes.printAdminHeader(templateContext),
require(file).main(request, clientAddress, templateContext, filename),
includes.printFooter(templateContext),
]).then(function(content)
{
result.write(whiskers.render(content[0], templateContext));
result.end();
}).catch(function(err)
{
console.log(err);
throw err;
});
}
var templateContext = Object();
Promise.all([includes.fetchTemplate("admin/adminMain.html"),
includes.printAdminHeader(templateContext),
require(file).main(request, clientAddress, templateContext, filename),
includes.printFooter(templateContext),
]).then(function(content)
{
result.write(whiskers.render(content[0], templateContext));
result.end();
}).catch(function(err)
{
console.log(err);
throw err;
});
} }
}; };

+ 1
- 1
templates/admin/adminMain.html View File

@ -26,7 +26,7 @@
<h2>Login</h2> <h2>Login</h2>
</div> </div>
<form action="/admin/" method ="post" class="p-2">
<form action="/admin/login" method ="post" class="p-2">
<div class="form-group"> <div class="form-group">
<label for="username1">User Name</label> <label for="username1">User Name</label>
<input class="form-control" type="text" id="username1" name="username" placeholder="Enter username" required> <input class="form-control" type="text" id="username1" name="username" placeholder="Enter username" required>

+ 46
- 0
utils/utils.js View File

@ -6,6 +6,9 @@
//used for file io //used for file io
const fs = require('fs'); const fs = require('fs');
const whiskers = require('whiskers');
const includes = require("../includes/includes"); const includes = require("../includes/includes");
@ -118,6 +121,49 @@ module.exports=
result.write(html); result.write(html);
result.end(); result.end();
}) })
},
constructAdminPage: function(request, result, templateFiller)
{
var templateContext = Object();
var promises = [];
promises.push(includes.fetchTemplate("admin/adminMain.html"));
promises.push(includes.printAdminHeader(templateContext));
promises.push(includes.printFooter(templateContext));
if(module.exports.loggedIn(request))
{
promises.push(templateFiller(templateContext));
}
else
{
//login
const clientAddress = (request.headers['x-forwarded-for'] || '').split(',')[0]
|| request.connection.remoteAddress;
promises.push(require("../admin/login").main(request, clientAddress,templateContext));
}
Promise.all(promises).then(function(content)
{
result.write(whiskers.render(content[0], templateContext));
result.end();
}).catch(function(err)
{
console.log(err);
throw err;
});
},
adminPostRoute: function(request, result, templateFiller)
{
},
loggedIn(request)
{
return(request.session && request.session.user);
} }
}; };

Loading…
Cancel
Save