Quantcast
Channel: MyBB Community Forums - Tutorials
Viewing all articles
Browse latest Browse all 690

Logout redirects back to current page

$
0
0
Mybb's default logout redirect is index.php, this quick javascript will help to redirect the member back to the current page instead of forum index.

go intotemplate header_welcomeblock_member and find
 
href="{$mybb->settings['bburl']}/member.php?action=logout&logoutkey={$mybb->user['logoutkey']}"

change it to
 
href="#" data-logout-url="{$mybb->settings['bburl']}/member.php?action=logout&logoutkey={$mybb->user['logoutkey']}"

then add this at the bottom of the template
 
<script>
  document.addEventListener("DOMContentLoaded", function () {
    const logoutLink = document.querySelector(".logout-link");
    if (logoutLink) {
      logoutLink.addEventListener("click", function (e) {
        e.preventDefault();
        const logoutUrl = logoutLink.getAttribute("data-logout-url");

        // Save current page in session storage
        sessionStorage.setItem("afterLogoutRedirect", location.pathname + location.search);

        // Now go to the logout URL (MyBB handles this)
        window.location.href = logoutUrl;
      });
    }

    // After logout, if there's a redirect URL stored, go back to it
    const redirect = sessionStorage.getItem("afterLogoutRedirect");
    if (redirect && !document.referrer.includes("member.php")) {
      sessionStorage.removeItem("afterLogoutRedirect");
      // Delay slightly to make sure logout is complete
      setTimeout(() => {
        window.location.href = redirect;
      }, 100);
    }
  });
</script>


Viewing all articles
Browse latest Browse all 690

Trending Articles