Skip to content

Commit

Permalink
fixed some content fetching issues!!
Browse files Browse the repository at this point in the history
  • Loading branch information
neeraj-395 committed Mar 15, 2024
1 parent 1b74d8d commit a0fc92c
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 73 deletions.
90 changes: 50 additions & 40 deletions assets/js/common.js
Original file line number Diff line number Diff line change
@@ -1,74 +1,76 @@
// BASE URL FOR FETCHING CONTENT
var baseURL = window.location.protocol + "//" + window.location.host;
var baseURL = getBaseURL('/nsp-dbms-project'); // Define root folder name with forward slash (/) !importatant

//---------------------Load Header And Footer Into HTML Document---------------------//
$(function(){
$(function () {
var header = $.Deferred();
var footer = $.Deferred();
$('#symbol').load(baseURL + "/assets/img/symbols.svg"); // load essentials symbols
$('#header').load(baseURL + "/partials/header.html", function() {
$('#header').load(baseURL + "/partials/header.html", function () { header.resolve(); }); // load header
$('#footer').load(baseURL + "/partials/footer.html", function () { footer.resolve(); }); // load footer

$.when(header, footer).done(function () {
//-------Check The Login Status of Current User-------//
$.ajax({
url: baseURL + "/backend/status.inc.php",
dataType: 'json',
success: function(result) {
success: function (result) {
if (result.isLoggedIn) {
var div = $('#status');
div.find('a').eq(0).attr('href', '#').text('Profile');
div.find('a').eq(1).attr('href', baseURL + "/backend/logout.inc.php").text('Log Out');
}
},
error: function(xhr, status, error) {
error: function (xhr, status, error) {
console.error('There was a problem with the fetch operation:', error);
}
});
//----Convert Root-Relative-Path To Absolute Links----//
$('a[href^="/"], img[src^="/"]').each(function() {

//----Convert Root-Relative-Path To relative Links----//
$('a[href^="/"], img[src^="/"]').each(function () {
var filepath = $(this).attr('href');
var imgpath = $(this).attr('src');
// Update the href attribute with the relative path
if(filepath) $(this).attr('href', baseURL + filepath);
if(imgpath) $(this).attr('src', baseURL + imgpath)
if (filepath) $(this).attr('href', baseURL + filepath);
if (imgpath) $(this).attr('src', baseURL + imgpath)
});
}); // load header
//-----------------------------------------------------//
$('#footer').load(baseURL + "/partials/footer.html"); // load footer
})
});

document.addEventListener('DOMContentLoaded', ()=>{
//---------------------Load Notes Content into HTML Document---------------------//

// Add note cards along with thier meta data
if(container = document.getElementById('recent-notes')) {
const meta = {
title : "Card title",
desc : "This is a wider card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.",
upload_time : "Last updated 3 mins ago",
path : baseURL + "/assets/img/note-img.jpg"
}

for (let i = 0; i < 6; i++)
container.innerHTML += recent_notes(meta.title,meta.desc,meta.upload_time,meta.path);

document.addEventListener('DOMContentLoaded', () => {
//---------------------Load Notes Content into HTML Document---------------------//
if (container = document.getElementById('recent-notes')) {
const meta = {
title: "Card title",
desc: "This is a wider card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.",
upload_time: "Last updated 3 mins ago",
path: baseURL + "/assets/img/note-img.jpg"
}
// Add note cards along with thier meta data
if(container = document.getElementById('content')) {
const meta = {
desc : "This is a wider card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.",
upload_time : "9 min",
vlink : "#", // view link
dlink : "#" // download link
}

// Add note cards along with there meta data
for (let i = 0; i < 6; i++)
container.innerHTML += recent_notes(meta.title, meta.desc, meta.upload_time, meta.path);

for (let i = 0; i < 9; i++)
container.innerHTML += search_notes(meta.desc,meta.upload_time,meta.vlink,meta.dlink);
}
if (container = document.getElementById('content')) {
const meta = {
desc: "This is a wider card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.",
upload_time: "9 min",
vlink: "#", // view link
dlink: "#" // download link
}

// Add note cards along with thier meta data
for (let i = 0; i < 9; i++)
container.innerHTML += search_notes(meta.desc, meta.upload_time, meta.vlink, meta.dlink);
}
});


//___________________ Utils ___________________ //

function recent_notes(title, desc, time, img_link) {
return `
return `
<div class="container-sm col">
<div class="card mb-3" style="max-width: 540px;">
<div class="row g-0">
Expand All @@ -89,7 +91,7 @@ function recent_notes(title, desc, time, img_link) {
}

function search_notes(note_desc, time, view_link, download_link) {
return `
return `
<div class="col">
<div class="card shadow-sm">
<svg class="bd-placeholder-img card-img-top" width="100%" height="225" xmlns="http://www.w3.org/2000/svg" role="img" aria-label="Placeholder: Thumbnail" preserveAspectRatio="xMidYMid slice" focusable="false"><title>Placeholder</title><rect width="100%" height="100%" fill="#55595c"/><text x="50%" y="50%" fill="#eceeef" dy=".3em">Thumbnail</text></svg>
Expand All @@ -107,4 +109,12 @@ function search_notes(note_desc, time, view_link, download_link) {
</div>
</div>
`;
}

function getBaseURL(rootFolderName) {
if(window.location.pathname.includes(rootFolderName)){
return window.location.origin + rootFolderName;
} else {
return window.location.origin;
}
}
52 changes: 42 additions & 10 deletions auth/auth.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';
// BASE URL FOR FETCHING CONTENT
var baseURL = window.location.protocol + "//" + window.location.host;
var baseURL = getBaseURL('/nsp-dbms-project'); // Define root folder name with forward slash (/) !importatant

$(function () {
$("input[type='password'][data-eye]").each(function (i) {
Expand Down Expand Up @@ -49,7 +49,7 @@ $(function () {
$this.attr('type', 'password');
$this.removeClass("show");
$(this).html($("<img>",{
src: baseURL + '/assets/img/eye-show.svg',
src: baseURL = '/assets/img/eye-show.svg',
height: '24px',
width: '24px'
}));
Expand All @@ -58,7 +58,7 @@ $(function () {
$this.val($("#passeye-" + i).val());
$this.addClass("show");
$(this).html($("<img>",{
src: baseURL + '/assets/img/eye-hide.svg',
src: baseURL + '/assets/img/eye-hide.svg',
height: '24px',
width: '24px'
}));
Expand Down Expand Up @@ -137,23 +137,55 @@ function authenticate(form, filepath) {
})
.then(result => {
switch(result.status){
case 200: alert(result.message); // good reponse
case 200: if(result.message.length) alert(result.message);
if(result.redirect) window.location.href = baseURL + result.redirect;
break;
case 300: //good response
if(result.redirect) window.location.href = baseURL + result.redirect;
break;
case 404: alert(result.message); // bad response
case 404: alert(result.message); // Bad response
window.location.reload();
break;
case 500: alert(result.message); // bad response
case 500: alert(result.message); // Warning Reponse
window.location.reload();
break;
default: alert("Unexpected error occurred. Please try again later :-(");
window.location.reload();
break;
}
})
.catch(error => {
console.error('There was a problem with the fetch operation:', error);
err_msg = `An error has occurred, most likely due to an attempt to execute server-side scripts on a GitHub page, which is not permitted.
Please run this project on a PHP-supported server for seamless functionality.
Thank you for your understanding and cooperation.\n Ignore: ${error}`
alert(err_msg);
window.location.reload();
});
}

function custom_alert(status_code, message) {
var alert_type = {
200 : ' alert alert-success alert-dismissible fade show',
404 : 'alert alert-danger alert-dismissible fade show',
500 : 'alert alert-warning alert-dismissible fade show'
}

var div = document.createElement('div');
div.setAttribute('class', alert_type[status_code]);
div.setAttribute('role', 'alert');
div.innerHTML = message;

var btn = document.createElement('button');
btn.setAttribute('type', 'button');
btn.setAttribute('class', 'btn-close');
btn.setAttribute('data-bs-dismiss', 'alert');
btn.setAttribute('aria-label', 'Close');
div.appendChild(btn);

document.body.appendChild(div);
}

function getBaseURL(rootFolderName) {
if(window.location.pathname.includes(rootFolderName)){
return window.location.origin + rootFolderName;
} else {
return window.location.origin;
}
}
6 changes: 3 additions & 3 deletions auth/html/forgot.html → auth/pages/forgot.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@
<title>Forgot&dash;Password</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<link rel="stylesheet" type="text/css" href="../auth.css">
<link rel="icon" href="/assets/img/logo.ico" type="image/x-icon">
<link rel="icon" href="../../assets/img/logo.ico" type="image/x-icon">
</head>
<body class="authentication">
<section class="h-100">
<div class="container h-100">
<div class="row justify-content-md-center align-items-center h-100">
<div class="card-wrapper">
<div class="brand">
<a href="/index.html" class="text-decoration-none link-body-emphasis">
<img src="/assets/img/logo.svg" alt="logo">
<a href="../../index.html" class="text-decoration-none link-body-emphasis">
<img src="../../assets/img/logo.svg" alt="logo">
</a>
</div>
<div class="card fat">
Expand Down
6 changes: 3 additions & 3 deletions auth/html/login.html → auth/pages/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<title>Login</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<link rel="stylesheet" type="text/css" href="../auth.css">
<link rel="icon" href="/assets/img/logo.ico" type="image/x-icon">
<link rel="icon" href="../../assets/img/logo.ico" type="image/x-icon">
</head>

<body class="authentication">
Expand All @@ -15,8 +15,8 @@
<div class="row justify-content-md-center h-100">
<div class="card-wrapper">
<div class="brand">
<a href="/index.html" class="text-decoration-none link-body-emphasis">
<img src="/assets/img/logo.svg" alt="logo">
<a href="../../index.html" class="text-decoration-none link-body-emphasis">
<img src="../../assets/img/logo.svg" alt="logo">
</a>
</div>
<div class="card fat">
Expand Down
6 changes: 3 additions & 3 deletions auth/html/reset.html → auth/pages/reset.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
<title>Reset</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<link rel="stylesheet" type="text/css" href="../auth.css">
<link rel="icon" href="/assets/img/logo.ico" type="image/x-icon">
<link rel="icon" href="../../assets/img/logo.ico" type="image/x-icon">
</head>
<body class="authentication">
<section class="h-100">
<div class="container h-100">
<div class="row justify-content-md-center align-items-center h-100">
<div class="card-wrapper">
<div class="brand">
<a href="/index.html" class="text-decoration-none link-body-emphasis">
<img src="/assets/img/logo.svg" alt="logo">
<a href="../../index.html" class="text-decoration-none link-body-emphasis">
<img src="../../assets/img/logo.svg" alt="logo">
</a>
</div>
<div class="card fat">
Expand Down
6 changes: 3 additions & 3 deletions auth/html/signup.html → auth/pages/signup.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
<title>Registration</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<link rel="stylesheet" type="text/css" href="../auth.css">
<link rel="icon" href="/assets/img/logo.ico" type="image/x-icon">
<link rel="icon" href="../../assets/img/logo.ico" type="image/x-icon">
</head>
<body class="authentication">
<section class="h-100">
<div class="container h-100">
<div class="row justify-content-md-center h-100">
<div class="card-wrapper">
<div class="brand">
<a href="/index.html" class="text-decoration-none link-body-emphasis">
<img src="/assets/img/logo.svg" alt="logo">
<a href="../../index.html" class="text-decoration-none link-body-emphasis">
<img src="../../assets/img/logo.svg" alt="logo">
</a>
</div>
<div class="card fat">
Expand Down
10 changes: 2 additions & 8 deletions backend/login.inc.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
<?php
session_start();
// Check if the user is already logged in, if yes then redirect him to welcome page
if(isset($_SESSION["isLoggedIn"]) && $_SESSION["isLoggedIn"] === true){
EXIT_WITH_JSON(300, "", "/index.html");
exit;
}

require_once "../database/connect.php";
set_error_handler('HANDLE_EXCEPTIONS'); // for any unexpected error

Expand Down Expand Up @@ -39,13 +32,14 @@

if($stmt->fetch()){
if(password_verify($password, $hashed_password)){
session_start();
// Store data in session variables
$_SESSION["user_id"] = $id;
$_SESSION["username"] = $username;
$_SESSION["name"] = $name;
$_SESSION["isLoggedIn"] = true;

EXIT_WITH_JSON(300, "", "/index.html", $conn, $stmt);
EXIT_WITH_JSON(200, "", "/index.html", $conn, $stmt);
} else {
// Password is not valid, display a generic error message
$login_err = "Invalid username or password.";
Expand Down
2 changes: 1 addition & 1 deletion backend/signup.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
// Attempt to execute the prepared statement.
if($stmt->execute()){
$msg = "Congratulations! Your signup was successful.\nThank you for joining us!";
$redirect_to = "/auth/html/login.html";
$redirect_to = "/auth/pages/login.html";
EXIT_WITH_JSON(200, $msg, $redirect_to, $conn, $stmt);
} else {
$err_msg = "Oops! something went wrong. Please try again later.";
Expand Down
4 changes: 2 additions & 2 deletions partials/header.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@

<div id="status" class="nav g-md-2 text-end justify-content-center">
<a style="background-color:#2b3c85;" role="button"
class="btn me-2 rounded-5 text-white" href="/auth/html/login.html">Login</a>
class="btn me-2 rounded-5 text-white" href="/auth/pages/login.html">Login</a>
<a style="background-color:#f2a833;" role="button"
class="btn rounded-5 text-white hideOn" href="/auth/html/signup.html">Sign Up</a>
class="btn rounded-5 text-white hideOn" href="/auth/pages/signup.html">Sign Up</a>
</div>
</div>
</div>
Expand Down

0 comments on commit a0fc92c

Please sign in to comment.