SHARE
TWEET

formController.js ( hashPassword(pw) )

a guest Jun 11th, 2019 92 in 6 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. "use strict"
  2. const model = require("./model.js");
  3. // max allowed String length (per entry) overall
  4. const MAX_CHARAKTER_PER_ENTRY = 2000;
  5.  
  6. function loginUserPassword(form, res, callback) {
  7.     model.hashPassword(form.pw);
  8.     if (!isValidForm(form)) {
  9.         loginError(form, res);
  10.     } else {
  11.         model.isValidUser(form.user, userExist => {
  12.             if(!userExist) {
  13.                 let login = { invalidUser:true };
  14.                 loginError(form, res, login);
  15.             } else {
  16.                 model.comparePassword(form.pw, form.user, isValid => {
  17.                     if(isValid) {
  18.                         model.getUserByName(form.user, cb => {
  19.                             callback(cb[0]);
  20.                         });
  21.                     } else {
  22.                         let login = { invalidPW:true };
  23.                         loginError(form, res, login);
  24.                     }
  25.                 });
  26.             }
  27.         });
  28.     }
  29. }
  30.  
  31. function loginError(form, res, login) {
  32.     console.log("Failed (login). Render with ERROR");
  33.     //ToDo: Save user input in data
  34.     let error = buildErrorObject(form, login);
  35.     res.render("login", { error, form });
  36. }
  37.  
  38. function addPost(form, res) {
  39.     if (isValidForm(form)) {
  40.         let post = formatPost(form);
  41.         model.addPost(post);
  42.         res.redirect("/");
  43.     } else {
  44.         console.log("Failed (addPost). Render with ERROR");
  45.         let error = buildErrorObject(form);
  46.         // This is so dirty :'(
  47.         let formInArray = [];
  48.         formInArray.push(form);
  49.         let data = [];
  50.         data.push(formInArray);
  51.         res.render("postForm", { error, data });
  52.     }
  53. }
  54.  
  55. function updatePost(id, form, res) {
  56.     if (isValidForm(form)) {
  57.         let post = formatPost(form);
  58.         model.updatePost(id, post);
  59.         res.redirect("/entry/" + id);
  60.     } else {
  61.         console.log("Failed (updatePost). Render with ERROR");
  62.         let error = buildErrorObject(form);
  63.         let formInArray = [];
  64.         formInArray.push(form);
  65.         let data = [];
  66.         data.push(formInArray);
  67.         res.render("postForm", { error, data });
  68.     }
  69. }
  70.  
  71. // Add comment for the Post with ID 'id'
  72. function addComment(id, form, res) {
  73.     if (isValidForm(form)) {
  74.         let comment = formatComment(form);
  75.         model.addComment(id, comment);
  76.         res.redirect("/entry/" + id);
  77.     } else {
  78.         console.log("Failed (addComment). Render with ERROR");
  79.         let error = buildErrorObject(form);
  80.         model.getPost(id, cb => {
  81.             let formInArray = [];
  82.             formInArray.push(form);
  83.             let data = [];
  84.             data.push(cb);
  85.             data.push(formInArray);
  86.             res.render("commentForm", { error, data });
  87.         });
  88.     }
  89. }
  90.  
  91. function updateComment(id, form, res) {
  92.     if (isValidForm(form)) {
  93.         let comment = formatComment(form);
  94.         model.updateComment(id, comment);
  95.         model.getComment(id, (cb) => {
  96.             res.redirect("/entry/" + cb.postId); // + cb.postId
  97.         });
  98.     } else {
  99.         console.log("Failed (updateComment). Render with ERROR");
  100.         model.getComment(id, (cb) => {
  101.             model.getPost(cb[0].postId, (wb) => {
  102.                 let formInArray = [];
  103.                 formInArray.push(form);
  104.                 let data = [];
  105.                 data.push(wb);
  106.                 data.push(formInArray);
  107.                 let error = buildErrorObject(form);
  108.                 res.render("commentForm", { error, data } );
  109.             });
  110.         });
  111.     }
  112. }
  113.  
  114. function formatPost(form) {
  115.     // Wenn Valide Eingabe:
  116.     let post = {
  117.         title: form.title,
  118.         code: form.code,
  119.         description: form.description,
  120.         author: form.author,
  121.         userId: form.userId,
  122.         languageId: form.languageId,
  123.         // Timestamp gets added at this point
  124.         date: new Date().toISOString().slice(0, 10)
  125.     };
  126.     return post;
  127. }
  128.  
  129. function formatComment(form) {
  130.     // Wenn Valide Eingabe:
  131.     let comment = {
  132.         title: form.title,
  133.         text: form.text,
  134.         userId: form.userId,
  135.         postId: form.postId,
  136.         // Timestamp gets added at this point
  137.         date: new Date().toISOString().slice(0, 10)
  138.     };
  139.     return comment;
  140. }
  141.  
  142. /*** Validation Functions ***/
  143. function isValidForm(form) {
  144.     for (var property in form) {
  145.         if (form[property] === '' && !isAllowedEmpty(property)) {
  146.             console.log('\x1b[33m%s\x1b[0m', "Invalid Form");
  147.             return false;
  148.         }
  149.         if (form[property].length > maxStringLenght(property)) {
  150.             console.log('\x1b[33m%s\x1b[0m', "Invalid Form");
  151.             return false;
  152.         }
  153.     }
  154.     console.log("valid form.");
  155.     return true;
  156. }
  157.  
  158. function isEmpty(property) {
  159.     if(property == '')
  160.         return true;
  161.     return false;
  162. }
  163.  
  164. function isAllowedEmpty(property) {
  165.     switch(property) {
  166.         case 'description':
  167.             return true;
  168.         case 'author':
  169.             return true;
  170.         default:
  171.             return false;
  172.     }
  173. }
  174.  
  175. function maxStringLenght(property) {
  176.     switch(property) {
  177.         case 'user':
  178.             return 20;
  179.         case 'title':
  180.             return 40;
  181.         case 'description':
  182.             return 1000;
  183.         case 'author':
  184.             return 100;
  185.         default:
  186.             return MAX_CHARAKTER_PER_ENTRY;
  187.     }
  188. }
  189.  
  190. function buildErrorObject(object, login) {
  191.     let error = {};
  192.     for (var property in object) {
  193.         if (isEmpty(object[property]) && !isAllowedEmpty(property)) {
  194.             error[property] = "Feld darf nicht leer sein";
  195.         }
  196.         else if (object[property].length > maxStringLenght(property)) {
  197.             error[property] = "Feld darf maximal " + maxStringLenght(property) + " Zeichen haben";
  198.         }
  199.     }
  200.     if(login != undefined) {
  201.         if (login['invalidUser']) {
  202.             error['user'] = "Username nicht gefunden";
  203.         }
  204.         if (login['invalidPW']) {
  205.             error['pw'] = "Passwort ung├╝ltig";
  206.         }
  207.     }
  208.     return error;
  209. }
  210.  
  211. exports.loginUserPassword = loginUserPassword;
  212. exports.addPost = addPost;
  213. exports.addComment = addComment;
  214. exports.updatePost = updatePost;
  215. exports.updateComment = updateComment;
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top