Skip to content

Commit

Permalink
added code.js.bak
Browse files Browse the repository at this point in the history
- Need this file for hasing
  • Loading branch information
jsalzer312 committed May 28, 2024
1 parent 3da33ac commit bbd3c21
Show file tree
Hide file tree
Showing 2 changed files with 185 additions and 0 deletions.
Binary file modified .DS_Store
Binary file not shown.
185 changes: 185 additions & 0 deletions js/code.js.bak
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
const urlBase = 'http://COP4331-5.com/LAMPAPI';
const extension = 'php';

let userId = 0;
let firstName = "";
let lastName = "";

function doLogin()
{
userId = 0;
firstName = "";
lastName = "";

let login = document.getElementById("loginName").value;
let password = document.getElementById("loginPassword").value;
// var hash = md5( password );

document.getElementById("loginResult").innerHTML = "";

let tmp = {login:login,password:password};
// var tmp = {login:login,password:hash};
let jsonPayload = JSON.stringify( tmp );

let url = urlBase + '/Login.' + extension;

let xhr = new XMLHttpRequest();
xhr.open("POST", url, true);
xhr.setRequestHeader("Content-type", "application/json; charset=UTF-8");
try
{
xhr.onreadystatechange = function()
{
if (this.readyState == 4 && this.status == 200)
{
let jsonObject = JSON.parse( xhr.responseText );
userId = jsonObject.id;

if( userId < 1 )
{
document.getElementById("loginResult").innerHTML = "User/Password combination incorrect";
return;
}

firstName = jsonObject.firstName;
lastName = jsonObject.lastName;

saveCookie();

window.location.href = "color.html";
}
};
xhr.send(jsonPayload);
}
catch(err)
{
document.getElementById("loginResult").innerHTML = err.message;
}

}

function saveCookie()
{
let minutes = 20;
let date = new Date();
date.setTime(date.getTime()+(minutes*60*1000));
document.cookie = "firstName=" + firstName + ",lastName=" + lastName + ",userId=" + userId + ";expires=" + date.toGMTString();
}

function readCookie()
{
userId = -1;
let data = document.cookie;
let splits = data.split(",");
for(var i = 0; i < splits.length; i++)
{
let thisOne = splits[i].trim();
let tokens = thisOne.split("=");
if( tokens[0] == "firstName" )
{
firstName = tokens[1];
}
else if( tokens[0] == "lastName" )
{
lastName = tokens[1];
}
else if( tokens[0] == "userId" )
{
userId = parseInt( tokens[1].trim() );
}
}

if( userId < 0 )
{
window.location.href = "index.html";
}
else
{
document.getElementById("userName").innerHTML = "Logged in as " + firstName + " " + lastName;
}
}

function doLogout()
{
userId = 0;
firstName = "";
lastName = "";
document.cookie = "firstName= ; expires = Thu, 01 Jan 1970 00:00:00 GMT";
window.location.href = "index.html";
}

function addColor()
{
let newColor = document.getElementById("colorText").value;
document.getElementById("colorAddResult").innerHTML = "";

let tmp = {color:newColor,userId,userId};
let jsonPayload = JSON.stringify( tmp );

let url = urlBase + '/AddColor.' + extension;

let xhr = new XMLHttpRequest();
xhr.open("POST", url, true);
xhr.setRequestHeader("Content-type", "application/json; charset=UTF-8");
try
{
xhr.onreadystatechange = function()
{
if (this.readyState == 4 && this.status == 200)
{
document.getElementById("colorAddResult").innerHTML = "Color has been added";
}
};
xhr.send(jsonPayload);
}
catch(err)
{
document.getElementById("colorAddResult").innerHTML = err.message;
}

}

function searchColor()
{
let srch = document.getElementById("searchText").value;
document.getElementById("colorSearchResult").innerHTML = "";

let colorList = "";

let tmp = {search:srch,userId:userId};
let jsonPayload = JSON.stringify( tmp );

let url = urlBase + '/SearchColors.' + extension;

let xhr = new XMLHttpRequest();
xhr.open("POST", url, true);
xhr.setRequestHeader("Content-type", "application/json; charset=UTF-8");
try
{
xhr.onreadystatechange = function()
{
if (this.readyState == 4 && this.status == 200)
{
document.getElementById("colorSearchResult").innerHTML = "Color(s) has been retrieved";
let jsonObject = JSON.parse( xhr.responseText );

for( let i=0; i<jsonObject.results.length; i++ )
{
colorList += jsonObject.results[i];
if( i < jsonObject.results.length - 1 )
{
colorList += "<br />\r\n";
}
}

document.getElementsByTagName("p")[0].innerHTML = colorList;
}
};
xhr.send(jsonPayload);
}
catch(err)
{
document.getElementById("colorSearchResult").innerHTML = err.message;
}

}

0 comments on commit bbd3c21

Please sign in to comment.