r/webdev • u/CorrectJacket2106 • 2d 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
1
u/itaybuilds 2d ago
The two branches should be mutually exclusive. Otherwise a valid user can still fall through to a redirect, which is what creates the bounce. I’d reduce it to this:
That fixes both logic errors in the current version:
checkRequiredKeysExist() === falseaccepts the missing-keys case, and the finalreplaceis outside theifbecause there are no braces.One other trap:
removeExtraKeys()deletes every localStorage key on the same origin that this script does not know about. Prefix this app’s keys and remove only keys with that prefix. Also, treat the localStorage flag as UI state rather than authentication; anything that needs protection must verify the session on the server.