Hello I have this error where the toast alert is not showing on the page when I log in but it is when I sign out, any idea why it does this? I tried multiple things like logging the toastLiveExample and the toastBody but it always returns null when I sign in showToast("text-bg-success", ${username}, you have successfully signed in! Check out your <a href="/profile/${username}">profile</a>!);
TypeError: Cannot read properties of null (reading ‘classList’)
at showToast (toast.js:6:20)
at HTMLDocument. (sign-in?id=…)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>GildNovel</title>
<link rel="icon" type="icon" href="/images/icon.jpg" />
<meta property="og:site_name" content="GildNovel" />
<meta property="og:title" content="Novel and Manga Community" />
<meta property="og:description"
content="GildNovel is a platform where you can find a variety of books and novels up to your taste, specially made for manga-lovers and action lovers." />
<meta property="og:image" content="/images/icon.jpg" />
<meta property="og:image:type" content="image/png">
<meta property="og:image:width" content="250">
<meta property="og:image:height" content="250">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
<link rel="stylesheet" href="https://site-assets.fontawesome.com/releases/v6.1.1/css/all.css" />
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Bebas+Neue&display=swap" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" />
<link rel="stylesheet" href="/gnCss" />
</head>
<style>
/*Buttons*/
.flex {
display: flex;
justify-content: center;
flex-direction: column;
align-items: center;
}
button {
text-align: center;
font-weight: bold;
text-decoration: none;
font-family: "Roboto", sans-serif;
display: inline-block;
border: none;
border-radius: 16px;
background-color: #2196f3;
cursor: pointer;
padding: 20px;
font-size: 30px;
color: white;
margin: 0 auto;
margin-bottom: 10px;
}
.discordButton {
background-color: #7289da;
color: #ffffff;
border: none;
border-radius: 15px;
font-size: 25px;
padding: 15px;
cursor: pointer;
transition: background-color 0.3s ease;
}
.discordButton:hover {
background-color: #677bc4;
}
.googleButton {
background-color: #fff;
color: #000;
border: none;
border-radius: 15px;
font-size: 25px;
padding: 15px;
cursor: pointer;
transition: background-color 0.3s ease;
}
.googleButton:hover {
background-color: #b7b7b7;
}
</style>
<body>
<div id="alert"></div>
<div class="navbar">
<a style="margin-left: 25px;" href="/home" class="navbar-text">Home</a>
<form name="search" class="box" action="/explore" method="GET">
<input type="text" name="search" style="width: 40vw;" placeholder="Explore..." />
<i class="fas fa-search"></i>
</form>
<div class="nav-right">
<a style="margin-right: 25px;" href="/premium" class="navbar-text">Premium</a>
<a style="margin-right: 25px;" id="toggle" href="#" class="navbar-text"><i class="fa fa-sign-out"
aria-hidden="true"></i></a>
<a style="margin-right: 25px;" id="account" href="#" class="navbar-text"><i class="fas fa-user"></i></a>
</div>
</div>
<script>
if (typeof Storage !== "undefined") {
if (localStorage.user) {
let data = JSON.parse(localStorage.user);
document.getElementById("account").href = `/profile/${data.username}`;
} else {
document.getElementById("toggle").innerHTML = '<i class="fa fa-sign-in" aria-hidden="true"></i>';
document.getElementById("toggle").href = "/sign-in";
}
}
document.getElementById("toggle").addEventListener("click", async (e) => {
if (e.target.classList.contains('fa-sign-out')) {
showToast("text-bg-success", "Signing you out.");
localStorage.removeItem("user");
await fetch('/logout', { method: 'POST', credentials: 'same-origin' })
setTimeout(() => {
window.location.reload();
}, 3000);
}
});
</script>
<div class="header">
<h1 class="bebas-neue-regular" style="font-size:15vw;">GILDNOVEL</h1>
<p>Experience the full potential of our community by joining or signing in with Discord or Google!</p>
</div>
<div style="margin-top:50px;" class="flex">
<button id="discordSignUp" class="discordButton">
<i class="fa-brands fa-discord"></i> Sign in with Discord
</button>
<button id="googleSignIn" class="googleButton">
<i class="fa-brands fa-google"></i> Sign in with Google
</button>
</div>
<div class="header-no-image">
<p>By signing in you agree to follow our <a href="/tos">Terms of Service</a></p>
</div>
<!--bootstrap code-->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js"></script>
<!--alert code-->
<script type="text/javascript" src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script>
$("#alert").load("/staticHtml/toast.html");
</script>
<!-- toast function -->
<script src="/js/toast.js"></script>
<script>
const discordSignUpBtn = document.getElementById("discordSignUp");
discordSignUpBtn.addEventListener("click", () => {
const clientID = "1100495054063284354";
const redirectURI = encodeURIComponent("https://novels-production.up.railway.app/auth/discord/callback");
const scopes = encodeURIComponent("email identify");
const url = `https://discord.com/oauth2/authorize?client_id=${clientID}&redirect_uri=${redirectURI}&response_type=code&scope=${scopes}`;
window.location.href = url;
});
const googleSignUpBtn = document.getElementById("googleSignIn");
googleSignUpBtn.addEventListener("click", () => {
window.location.href = "https://novels-production.up.railway.app/auth/google";
});
</script>
<script>
document.addEventListener('DOMContentLoaded', async () => {
try {
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
const token = urlParams.get("token");
if (token) {
const id = urlParams.get("id");
const username = urlParams.get("username");
let params = {
_id: id,
username,
token,
};
showToast("text-bg-success", `${username}, you have successfully signed in! Check out your <a href="/profile/${username}">profile</a>!`);
localStorage.setItem("user", JSON.stringify(params));
}
} catch (err) {
console.log(err)
showToast("text-bg-danger", "Something went wrong");
}
});
</script>
</body>
</html>
function showToast(toastType, message) {
const toastLiveExample = document.getElementById("liveToast");
const toastBody = document.getElementById("toastBody");
const toastBootstrap = bootstrap.Toast.getOrCreateInstance(toastLiveExample);
toastLiveExample.classList.add(toastType);
toastBody.innerHTML = message;
toastBootstrap.show();
}
<div class="toast-container position-fixed bottom-0 end-0 p-3">
<div id="liveToast" class="toast align-items-center" role="alert" aria-live="assertive" aria-atomic="true">
<div class="d-flex">
<div class="toast-body" id="toastBody">Hello, world! This is a toast message.</div>
<i class="fa fa-times me-2 m-auto" data-bs-dismiss="toast" aria-hidden="true"></i>
</div>
</div>
</div>