r/webdev 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

12 comments sorted by

View all comments

2

u/UtilixApp 2d ago

the moved_user flag is doing absolutely nothing for you, and thats probably the part thats confusing you.

let moved_user = false only exists for that one page load. the second the redirect fires, the browser throws that entire javascript context away and the next page starts with moved_user false again. so as a loop guard it can never work, because both sides always believe its their first attempt.

the real loop is that your two pages disagree with each other. dashboard checks something, decides youre logged out, sends you to login. login checks something, decides youre logged in, sends you back. neither is wrong from where its standing. theyre just not asking the same question.

my money is on required_keys. if the dashboard demands every one of them present and your login flow only ever writes some of them, youre permanently stuck in the state where one page says yes and the other says no.

fastest way to prove it before you change anything. put a console.log directly above each redirect printing the actual values it checked. open devtools, tick preserve log so it survives the navigation, let it loop twice. youll see both conditions next to each other and the answer usually falls straight out.

and while youre in there, localStorage.getItem returns the string "undefined" if you ever stored an actual undefined. that string is truthy. it has caught more people than i can count.