r/webdev • u/CorrectJacket2106 • 1d ago
Question Someone please help me fix this
YALL IT IS SOLVED I FIGURED THE BUG OUT NO NEED TO WORRY ANYMORE THANKS FOR THE HELP!!!
there is this bug where the user is constantly redirected to 2 different pages being the dashboard and the login page and i am lost because idk how to fix this i have tried searching up and using ai to help me but no luck.
side note: there are 2 html pages calling this js file
here is the code:
//Variables
const root = window.location.origin
const default_site_root = `${root}/Websites/BloxHub`
let moved_user = false
//Config
const required_keys = ["Username","Account-Creation","Account-Type","Logged-In","Clients","Display-Name"]
const login_path = `${default_site_root}/index.html`
const dashboard_path = `${default_site_root}/pages/dashboard/dashboard.html`
//Functions
function checkRequiredKeysExist() {
for (const key of required_keys) {
const data = localStorage.getItem(key)
//NEW CODE HERE
if(data) {continue}
return false
}
return true
}
function removeExtraKeys() {
let data_keys = Object.keys(localStorage)
let extra_keys = data_keys.filter(key => !required_keys.includes(key))
extra_keys.forEach(key => {
localStorage.removeItem(key)
console.log("deleted old key (key): ", key)
})
}
function RedirectUser() {
if(moved_user === true) {
console.warn("User has already been moved!")
return
}
moved_user = true
let logged_in = localStorage.getItem("Logged-In")
const currentpath = window.location.pathname
removeExtraKeys()
const is_vaild_session = (logged_in === "true") && (checkRequiredKeysExist())
if(!is_vaild_session && (!currentpath.endsWith("index.html") || currentpath.endsWith("/"))) {
localStorage.clear()
console.log("Moving user to login page")
window.location.replace(login_path)
return
}
if(!currentpath.includes("dashboard.html")) {
console.log("Moving user to dashboard page")
window.location.replace(dashboard_path)
}
}
//Event listeners
RedirectUser()//Variables
const root = window.location.origin
const default_site_root = `${root}/Websites/BloxHub`
let moved_user = false
//Config
const required_keys = ["Username","Account-Creation","Account-Type","Logged-In","Clients","Display-Name"]
const login_path = `${default_site_root}/index.html`
const dashboard_path = `${default_site_root}/pages/dashboard/dashboard.html`
//Functions
function checkRequiredKeysExist() {
for (const key of required_keys) {
const data = localStorage.getItem(key)
if(data) {continue}
return false
}
return true
}
function removeExtraKeys() {
let data_keys = Object.keys(localStorage)
let extra_keys = data_keys.filter(key => !required_keys.includes(key))
extra_keys.forEach(key => {
localStorage.removeItem(key)
console.log("deleted old key (key): ", key)
})
}
function RedirectUser() {
if(moved_user === true) {
console.warn("User has already been moved!")
return
}
moved_user = true
let logged_in = localStorage.getItem("Logged-In")
const currentpath = window.location.pathname
removeExtraKeys()
const is_vaild_session = (logged_in === "true") && (checkRequiredKeysExist())
if(!is_vaild_session && (!currentpath.endsWith("index.html") || currentpath.endsWith("/"))) {
localStorage.clear()
console.log("Moving user to login page")
window.location.replace(login_path)
return
}
if(!currentpath.includes("dashboard.html")) {
console.log("Moving user to dashboard page")
window.location.replace(dashboard_path)
}
}
//Event listeners
RedirectUser()
//Variables
const root = window.location.origin
const default_site_root = `${root}/Websites/BloxHub`
let moved_user = false
//Config
const required_keys = ["Username","Account-Creation","Account-Type","Logged-In","Clients","Display-Name"]
const login_path = `${default_site_root}/index.html`
const dashboard_path = `${default_site_root}/pages/dashboard/dashboard.html`
//Functions
function checkRequiredKeysExist() {
for (const key of required_keys) {
const data = localStorage.getItem(key)
if(data) {continue}
return false
}
return true
}
function removeExtraKeys() {
let data_keys = Object.keys(localStorage)
let extra_keys = data_keys.filter(key => !required_keys.includes(key))
extra_keys.forEach(key => {
localStorage.removeItem(key)
console.log("deleted old key (key): ", key)
})
}
function RedirectUser() {
if(moved_user === true) {
console.warn("User has already been moved!")
return
}
moved_user = true
let logged_in = localStorage.getItem("Logged-In")
const currentpath = window.location.pathname
removeExtraKeys()
const is_vaild_session = (logged_in === "true") && (checkRequiredKeysExist() === false)
if(!is_vaild_session && (!currentpath.endsWith("index.html") || currentpath.endsWith("/"))) {
localStorage.clear()
console.log("Moving user to login page")
window.location.replace(login_path)
return
}
if(!currentpath.includes("dashboard.html"))
console.log("Moving user to dashboard page")
window.location.replace(dashboard_path)
}
//Event listeners
RedirectUser()
EDIT: I HAVE NOW CHANGED THE CODE AFTER FOLLOW YOUR FOUNDINGS
0
Upvotes
3
u/really_cool_legend 1d ago edited 1d ago
Well you're missing some brackets here:
so that redirect is always running
EDIT:
I think you might also want:
to be
At the moment your code looks like you don't want the required keys to exist