added api page
added login added api.ejs added middleware for authorization check
This commit is contained in:
88
consumerWebsite/public/js/api.js
Normal file
88
consumerWebsite/public/js/api.js
Normal file
@ -0,0 +1,88 @@
|
||||
|
||||
var elements = [];
|
||||
|
||||
[].forEach.call(document.querySelectorAll('.scroll-to-link'), function (div) {
|
||||
div.onclick = function (e) {
|
||||
e.preventDefault();
|
||||
var target = this.dataset.target;
|
||||
document.getElementById(target).scrollIntoView({ behavior: 'smooth' });
|
||||
var elems = document.querySelectorAll(".content-menu ul li");
|
||||
[].forEach.call(elems, function (el) {
|
||||
el.classList.remove("active");
|
||||
});
|
||||
this.classList.add("active");
|
||||
return false;
|
||||
};
|
||||
});
|
||||
|
||||
document.getElementById('button-menu-mobile').onclick = function (e) {
|
||||
e.preventDefault();
|
||||
document.querySelector('html').classList.toggle('menu-opened');
|
||||
}
|
||||
document.querySelector('.left-menu .mobile-menu-closer').onclick = function (e) {
|
||||
e.preventDefault();
|
||||
document.querySelector('html').classList.remove('menu-opened');
|
||||
}
|
||||
|
||||
function debounce (func) {
|
||||
var timer;
|
||||
return function (event) {
|
||||
if (timer) clearTimeout(timer);
|
||||
timer = setTimeout(func, 100, event);
|
||||
};
|
||||
}
|
||||
|
||||
function calculElements () {
|
||||
var totalHeight = 0;
|
||||
elements = [];
|
||||
[].forEach.call(document.querySelectorAll('.content-section'), function (div) {
|
||||
var section = {};
|
||||
section.id = div.id;
|
||||
totalHeight += div.offsetHeight;
|
||||
section.maxHeight = totalHeight - 25;
|
||||
elements.push(section);
|
||||
});
|
||||
onScroll();
|
||||
}
|
||||
|
||||
function onScroll () {
|
||||
var scroll = window.pageYOffset;
|
||||
console.log('scroll', scroll, elements)
|
||||
for (var i = 0; i < elements.length; i++) {
|
||||
var section = elements[i];
|
||||
if (scroll <= section.maxHeight) {
|
||||
var elems = document.querySelectorAll(".content-menu ul li");
|
||||
[].forEach.call(elems, function (el) {
|
||||
el.classList.remove("active");
|
||||
});
|
||||
var activeElems = document.querySelectorAll(".content-menu ul li[data-target='" + section.id + "']");
|
||||
[].forEach.call(activeElems, function (el) {
|
||||
el.classList.add("active");
|
||||
});
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (window.innerHeight + scroll + 5 >= document.body.scrollHeight) { // end of scroll, last element
|
||||
var elems = document.querySelectorAll(".content-menu ul li");
|
||||
[].forEach.call(elems, function (el) {
|
||||
el.classList.remove("active");
|
||||
});
|
||||
var activeElems = document.querySelectorAll(".content-menu ul li:last-child");
|
||||
[].forEach.call(activeElems, function (el) {
|
||||
el.classList.add("active");
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
calculElements();
|
||||
window.onload = () => {
|
||||
calculElements();
|
||||
};
|
||||
window.addEventListener("resize", debounce(function (e) {
|
||||
e.preventDefault();
|
||||
calculElements();
|
||||
}));
|
||||
window.addEventListener('scroll', function (e) {
|
||||
e.preventDefault();
|
||||
onScroll();
|
||||
});
|
@ -134,7 +134,6 @@ app.api = (function (app) {
|
||||
complete: function (res, text) {
|
||||
callback(
|
||||
text !== "success" ? res.statusText : null,
|
||||
//console.log(res.responseText),
|
||||
JSON.parse(res.responseText),
|
||||
res.status
|
||||
);
|
||||
@ -151,8 +150,9 @@ app.auth = (function (app) {
|
||||
localStorage.setItem("APIToken", token);
|
||||
}
|
||||
|
||||
function setUserId(userId) {
|
||||
localStorage.setItem("userId", userId);
|
||||
function setUserId(userid) {
|
||||
console.log("userid", userid);
|
||||
localStorage.setItem("userid", userid);
|
||||
}
|
||||
|
||||
function setUsername(username) {
|
||||
@ -185,25 +185,39 @@ app.auth = (function (app) {
|
||||
*/
|
||||
|
||||
function logOut(callback) {
|
||||
localStorage.removeItem("APIToken");
|
||||
localStorage.removeItem("userId");
|
||||
localStorage.removeItem("username");
|
||||
//call logout route
|
||||
$.ajax({
|
||||
type: "DELETE",
|
||||
url: "/api/v0/user/logout",
|
||||
headers: {
|
||||
"auth-token": app.auth.getToken(),
|
||||
},
|
||||
contentType: "application/json; charset=utf-8",
|
||||
dataType: "json",
|
||||
complete: function (res, text) {
|
||||
callback(
|
||||
text !== "success" ? res.statusText : null,
|
||||
JSON.parse(res.responseText),
|
||||
res.status
|
||||
);
|
||||
},
|
||||
});
|
||||
|
||||
//remove token from db NOT the api key.
|
||||
localStorage.removeItem("APIToken");
|
||||
localStorage.removeItem("userid");
|
||||
localStorage.removeItem("username");
|
||||
callback();
|
||||
}
|
||||
|
||||
function forceLogin() {
|
||||
$.holdReady(true);
|
||||
app.auth.isLoggedIn(function (error, isLoggedIn) {
|
||||
if (error || !isLoggedIn) {
|
||||
app.auth.logOut(function () {});
|
||||
location.replace(`/login`);
|
||||
} else {
|
||||
$.holdReady(false);
|
||||
}
|
||||
});
|
||||
}
|
||||
app.auth.isLoggedIn(function (error, isLoggedIn) {
|
||||
if (error || !isLoggedIn) {
|
||||
app.auth.logOut(function () {
|
||||
location.replace(`/login`);
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function logInRedirect() {
|
||||
window.location.href =
|
||||
@ -215,6 +229,18 @@ app.auth = (function (app) {
|
||||
window.location.href = location.href.replace(location.replace(`/`)) || "/";
|
||||
}
|
||||
|
||||
//if isLoggedin is true, redirect user away from login / register page
|
||||
function redirectIfLoggedIn() {
|
||||
$.holdReady(true);
|
||||
app.auth.isLoggedIn(function (error, isLoggedIn) {
|
||||
if (error || isLoggedIn) {
|
||||
location.replace(`/`);
|
||||
} else {
|
||||
$.holdReady(false);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
getToken: getToken,
|
||||
setToken: setToken,
|
||||
@ -226,6 +252,7 @@ app.auth = (function (app) {
|
||||
forceLogin,
|
||||
logInRedirect,
|
||||
homeRedirect,
|
||||
redirectIfLoggedIn,
|
||||
};
|
||||
})(app);
|
||||
|
||||
|
@ -1,44 +0,0 @@
|
||||
require('dotenv').config({ path: path.resolve(__dirname, '../../../.env') })
|
||||
|
||||
|
||||
document.addEventListener('DOMContentLoaded', () => {
|
||||
const form = document.getElementById('form');
|
||||
|
||||
// Set the new value for the access_key input field
|
||||
form.querySelector('input[name="access_key"]').value = process.env.emailKey;
|
||||
|
||||
form.addEventListener('submit', async (event) => {
|
||||
event.preventDefault(); // Prevent default form submission
|
||||
|
||||
// Create a FormData object to include the key
|
||||
const formData = new FormData(form);
|
||||
|
||||
// Submit the form using fetch API
|
||||
try {
|
||||
const response = await fetch('https://api.web3forms.com/submit', {
|
||||
method: 'POST',
|
||||
body: formData
|
||||
});
|
||||
|
||||
const result = await response.json();
|
||||
|
||||
// Handle the API response
|
||||
//console.log(result);
|
||||
|
||||
if (result.success) {
|
||||
// Form submitted successfully, display notification
|
||||
alert('Form submitted successfully!');
|
||||
location.reload()
|
||||
// You can replace the alert with your custom notification logic
|
||||
} else {
|
||||
// Form submission failed, display error notification
|
||||
alert('Form submission failed. Please try again.');
|
||||
// You can replace the alert with your custom error notification logic
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
//console.error('Error:', error);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
Reference in New Issue
Block a user