Skip to content
Open
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Shubham_Sharma/Practice/Practice/bin/**
Shubham_Sharma/Practice/Practice/obj/**
Shubham_Sharma/node Assignment/node_modules/**
**/bin/
**/obj/
7 changes: 7 additions & 0 deletions Shubham_Sharma/JWT Assignment/Assets/css/bootstrap.min.css

Large diffs are not rendered by default.

Large diffs are not rendered by default.

59 changes: 59 additions & 0 deletions Shubham_Sharma/JWT Assignment/Assets/css/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
body{
height: 100vh;
}
input{
width: 100%;
}

/*For Login Page*/
.lgn-1{
display: flex; /*display: none*/
justify-content: center;
height: 100%;
align-items: center;
}
.lgn-2{
display: flex;
flex-direction: column;
align-items: center;
padding: 10%;
width: fit-content;
}
.lgn-2 div{
width: 120%;
margin-bottom: 10%;
margin-left: 5%;
margin-right: 5%;
}

/*For Signup Page*/
.sgn-up-1{
display: none; /*display: flex*/
justify-content: center;
height: 100%;
align-items: center;
}
.sgn-up-2{
display: flex;
flex-direction: column;
align-items: center;
padding: 10%;
width: fit-content;
}
.sgn-up-2 div{
width: 120%;
margin-bottom: 10%;
margin-left: 5%;
margin-right: 5%;
}

/*Navbar Admin*/
.nav-Admin{
display: none;
}
/* Removing arrows from input number fieldset */
input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
-webkit-appearance: none;
margin: 0;
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions Shubham_Sharma/JWT Assignment/Assets/js/bootstrap.min.js

Large diffs are not rendered by default.

Large diffs are not rendered by default.

141 changes: 141 additions & 0 deletions Shubham_Sharma/JWT Assignment/Assets/js/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
var lgOutBtn = document.getElementById("logOut");
var allBtn = document.getElementById("allEmp");
var table = document.getElementById("adminTable");
var del = document.getElementById("deleteOne");

var COLUMN_TO_DISPLAY = 7;

//inserts data into the table
function drawTable(json) {
var colLength = COLUMN_TO_DISPLAY;

for(let j=0; j<json.length; j++){
var x = json[j];
var data = [j+1, x.empName, x.username, x.empPhone, x.empRole, x.empAddress, x.empProjectId];
var row = table.insertRow(j);
for(let i=0; i<colLength; i++){
var cell = row.insertCell(i);
cell.innerHTML = data[i];
}
}
}

//Decode JWT and return the Payload in JSON Format
const jsonDecoder = (token) => {
var base64Url = token.split('.')[1];
var base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');
var jsonPayload = decodeURIComponent(atob(base64).split('').map(function(c) {
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
}).join(''));

return JSON.parse(jsonPayload);
};

//When the window is loaded onto the browser, then it will check whether a token presents or not.
//If token is present, it will decode it and fetch the Guy's name and paste it in the NAVBAR. Then it will call getAll()
//Else it will redirect the GUY back to LOGIN PAGE.
window.onload = () => {
if (typeof(Storage) !== "undefined") {
const token = localStorage.getItem("JwtTOKEN");
if(token != null){
var jsonPayload = jsonDecoder(token);
document.getElementById("userName").innerHTML = jsonPayload.EmpName;
getAll();
}
else{
console.log("There is no JwtToken present");
window.location.href = "./index.html";
}
}
else {
alert("Sorry ! Your browser is not cool.");
}
}

//Called when Logout button is pressed and then it deletes the Jwt and diverts the page back to LOGIN
const lgout = () => {
if (typeof(Storage) !== "undefined") {
localStorage.removeItem("JwtTOKEN");
console.log("something is not working");
window.location.href = "./index.html";
} else {
// Sorry! No Web Storage support..
alert("Sorry ! Your browser is not cool.");
}
}

//Sends the HTTP request and returns the promise. On resolve, xhr is returned, from which we can retrieve the response
const sendHTTPReq = (method, url, data) => {
const promise = new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.open(method, url);
console.log(JSON.stringify(data));
xhr.responseType = 'json';
if(data){
xhr.setRequestHeader('Content-Type', 'application/json');
}
xhr.onload = () => {
resolve(xhr);
};
xhr.onerror = () => {
reject('Something went wrong');
}
xhr.send(JSON.stringify(data));
});
return promise;
}

//Deletes the employee and
const deleteEmployee = () => {
var usn = prompt("Enter username you want to delete");
if(usn != null){
data = {
JwT: localStorage.getItem("JwtTOKEN"),
Username: usn
};
sendHTTPReq("DELETE", "https://localhost:44305/api/Admin", data)
.then((responseData) => {
if(responseData.status == 200){
alert("User Deleted");
getAll();
}
else{
alert("Some Error Occured "+responseData.status);
}
});
}
};

function clearTableData() {
var first = table.firstElementChild;
while (first) {
first.remove();
first = table.firstElementChild;
}
}

const getAll = () => {
clearTableData();
const reqBody = {
"JwT": localStorage.getItem("JwtTOKEN")
}
sendHTTPReq('POST', "https://localhost:44305/api/Admin/getAllUsers", reqBody)
.then((responseData) => {
var json = responseData.response;
if(json != null){
drawTable(json);
}
else{
localStorage.removeItem("JwtTOKEN");
window.alert("Token is expired or Maybe is tampered! Please Login Again");
window.location.href = "./index.html";
}
})
.catch(err => {
console.log(err);
});
};

allBtn.addEventListener('click', getAll);
lgOutBtn.addEventListener('click', lgout);
del.addEventListener('click', deleteEmployee);
173 changes: 173 additions & 0 deletions Shubham_Sharma/JWT Assignment/Assets/js/signup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
var sign = document.getElementById("signUpBtn");
var login = document.getElementById("loginButton");

//regLinki is in LOGIN Page
var regLink = document.getElementById("toRegistration");
//logLink is in SIGNUP PAGE
var logLink = document.getElementById("toLogin");

//Getting data from SIGNUP form and storing it in a JSON format
const getDataFromForm = () => {
var name = document.getElementById("name").value;
var phone = document.getElementById("phn").value;
var address = document.getElementById("addrs").value;
var usn = document.getElementById("usn").value;
var psswd = document.getElementById("pswd").value;
console.log(psswd);
var userData = {
"EmpName": name,
"Username": usn,
"EmpPhone": phone,
"EmpAddress": address,
"EmpRole": "EMPLOYEE",
"EmpPassword": psswd,
"EmpProjectId": "Bench",
"AdminFlag": 0,
"EmpFlag": 0
};
// console.log(JSON.stringify(json));
sendData(userData);
}

//Getting data from LOGIN form and storing it in a JSON format
const getDataFromLoginForm = () => {
var usn = document.getElementById("email").value;
var paswd = document.getElementById("lgnpswd").value;
console.log(paswd);
var usrInfo = {
"Username": usn,
"EmpPassword": paswd
};
logMeIn(usrInfo);
}

//Sending HTTP REQUESTS, other methods can call me and pass me the required information and I'll do the rest
const sendHTTPReq = (method, url, data) => {
const promise = new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.open(method, url);
console.log(JSON.stringify(data));
xhr.responseType = 'json';
if(data){
xhr.setRequestHeader('Content-Type', 'application/json');
}

xhr.onload = () => {
//resolve(xhr.status + "--Token--"+xhr.response.token);
resolve(xhr);
};
xhr.onerror = () => {
reject('Something went wrong');
}
xhr.send(JSON.stringify(data));
});
return promise;
}

//Storing the token in local storage
function saveInLocalStorage(ResToken){
if (typeof(Storage) !== "undefined") {
// Code for localStorage/sessionStorage.
localStorage.setItem("JwtTOKEN", ResToken);
console.log("Token is stored successfully");
} else {
// Sorry! No Web Storage support..
alert("Sorry ! Your browser is not cool.");
}
}

//Also for POST request, called from login page only
const logMeIn = (lgndata) => {
sendHTTPReq('POST', "https://localhost:44305/api/Login", lgndata)
.then(responseData => {
if(responseData.status == 200){
saveInLocalStorage(responseData.response.token);
var x = jsonDecoder(responseData.response.token);
if(x.EmpRole == "ADMIN")
window.location.href = "./admin-page.html";
else if(x.EmpRole == "EMPLOYEE")
window.location.href = "#";
else
window.location.href = "#";
}
else if(responseData.status == 401){
window.alert("Unauthorized");
}
else{
window.alert(responseData.status);
}
})
}

//For Post request, called from signup page only
const sendData = (userdata) => {

// sendHTTPReq('POST', "https://localhost:44305/api/Signup", userdata)
sendHTTPReq('POST', "http://localhost:8080/saveEmployee", userdata)
.then(responseData => {
console.log(responseData);
console.log(responseData.status);
if(responseData.status == 200){
document.getElementById("sgnup1").style.display = "none";
document.getElementById("lgn").style.display = "flex";
}
else{
window.alert(responseData.status);
}
})
.catch(err => {
console.log(err);
});
}



const jsonDecoder = (token) => {
var base64Url = token.split('.')[1];
var base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');
var jsonPayload = decodeURIComponent(atob(base64).split('').map(function(c) {
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
}).join(''));

return JSON.parse(jsonPayload);
};

window.onload = () => {
if (typeof(Storage) !== "undefined") {
// Code for localStorage/sessionStorage.
const token = localStorage.getItem("JwtTOKEN");
if(token != null){
var jsonPayload = jsonDecoder(token);
if(jsonPayload.EmpRole == "ADMIN")
window.location.href = "./admin-page.html";
else if(jsonPayload.EmpRole == "EMPLOYEE")
window.location.href = "";
else
window.location.href = "";
}
else{
console.log("There is no JwtToken present");
}
} else {
// Sorry! No Web Storage support..
alert("Sorry ! Your browser is not cool.");
}
}

//Attaching listeners to LOGIN Button and SIGNUP Button
sign.addEventListener('click', getDataFromForm);
login.addEventListener('click', getDataFromLoginForm);

// For Links
var l = () => {
document.getElementById("sgnup1").style.display = "none";
document.getElementById("lgn").style.display = "flex";
}
//For Links
var s = () => {
document.getElementById("lgn").style.display = "none";
document.getElementById("sgnup1").style.display = "flex";
}

logLink.addEventListener('click', l);
regLink.addEventListener('click', s);
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading