Skip to main content

How to fix redirect to dashboard being intercepted by pricing page in MemberStack and Dorik?

I am using memberstack to gate pages on a website I built using dorik. Dorik has made it very difficult and I can't use memberstack data attributes on my log in/out buttons. I have used custom code to get these buttons to work. and they work great, hide/ appear depending on login behaviour but for some reason when I log in, rather that redirecting to dashboard, it is taking me down to my pricing table and replacing the /affiliate-dashboard slug with /#pricing. I have checked my redirect options in member stack and they are all correct. in the console it is returning these errors : 

Unhandled Promise Rejection: [object Object]

 

• Unhandled Promise Rejection: [object Object]

 

Unhandled Promise Rejection: [object Object]

 

Unhandled Promise Rejection: [object Object]

 

Unhandled Promise Rejection: [object Object]

 

Refused to set unsafe header "cookie"

 

 

here is the custom code I used: 

 

<script>
document.addEventListener("DOMContentLoaded", async function () {

  const dashboardUrl = "/affiliate-dashboard";

  function getLinks() {
    return {
      login: document.querySelector('a[href*="/login"]'),
      signup: document.querySelector('a[href*="/signup"]'),
      logout: document.querySelector('a[href*="/logout"]')
    };
  }

  function hide(el) {
    if (el) el.style.display = "none";
  }

  function show(el) {
    if (el) el.style.display = "";
  }

  async function updateNavButtons() {

    const { login, signup, logout } = getLinks();

    const ms = window.$memberstackDom;

    if (!ms) return;

    const member = await ms.getCurrentMember();

    if (member && member.data) {

      hide(login);
      hide(signup);
      show(logout);

    } else {

      show(login);
      show(signup);
      hide(logout);

    }
  }

  // Wait for Memberstack to load
  let attempts = 0;

  const waitForMemberstack = setInterval(async function () {

    attempts++;

    if (window.$memberstackDom) {

      clearInterval(waitForMemberstack);

      await updateNavButtons();

    }

    if (attempts > 20) {

      clearInterval(waitForMemberstack);

      console.error("Memberstack did not load");

    }

  }, 300);

  // Handle button clicks
  document.addEventListener("click", async function (e) {

    const link = e.target.closest("a");

    if (!link) return;

    const href = link.href;

    const ms = window.$memberstackDom;

    if (!ms) return;

    // LOGIN
    if (href.includes("/login")) {

      e.preventDefault();

      await ms.openModal("LOGIN");

      // Watch for successful login
      const checkLogin = setInterval(async function () {

        const member = await ms.getCurrentMember();

        if (member && member.data) {

          clearInterval(checkLogin);

          window.location.href = dashboardUrl;

        }

      }, 500);

    }

    // SIGNUP
    if (href.includes("/signup")) {

      e.preventDefault();

      await ms.openModal("SIGNUP");

    }

    // LOGOUT
    if (href.includes("/logout")) {

      e.preventDefault();

      await ms.logout();

      window.location.href = "/";

    }

  });

});
</script>

 

 

any ideas on what is happening, is It something to do with Dorik intercepting the redirect?

 

   

1 comment

  • Duncan from Memberstack
    Duncan from Memberstack

    I talked with rey.memberstack.com and received the following advice. I think it's good advice (based on the data provided.) I don't know anything about Dorik and we've never tested Memberstack on a Dorik site:

    Root Cause of the Redirect Issue

    Your custom code is not explicitly setting a post-login redirect URL in the openModal("LOGIN") call. When you use ms.openModal("LOGIN") without specifying a redirect, Memberstack defaults to the first redirect URL configured in your Memberstack dashboard (likely your pricing page or homepage).

    Additionally, the "Unhandled Promise Rejection" errors and "Refused to set unsafe header 'cookie'" warnings suggest:

    1. Dorik’s JavaScript environment is blocking cookie settings (required for Memberstack authentication) due to security restrictions.
    2. The redirect chain is being intercepted by Dorik’s routing system, causing the URL to change from /affiliate-dashboard to /#pricing.


    Solution: Explicitly Set Redirects in Your Code

    Update your custom code to force the redirect after login by:

    1. Using ms.loginMemberEmailPassword() or ms.openModal() with a redirectUrl parameter.
    2. Avoiding window.location.href after login (since Dorik may interfere).

    Updated Code Example:

    document.addEventListener("DOMContentLoaded", async function () {
      const dashboardUrl = "/affiliate-dashboard";
      const ms = window.$memberstackDom;
    
      if (!ms) {
        console.error("Memberstack DOM library not loaded!");
        return;
      }
    
      // Initialize Memberstack with explicit redirect settings
      const memberstack = ms.init({
        publicKey: "YOUR_PUBLIC_KEY", // Replace with your actual public key
        appId: "YOUR_APP_ID", // Optional but recommended
        useCookies: true, // Critical for authentication
        redirectUrl: dashboardUrl, // Default redirect after login/logout
      });
    
      // Safely wait for Memberstack to initialize
      let attempts = 0;
      const waitForMemberstack = setInterval(async function () {
        attempts++;
        if (window.$memberstackDom?.getCurrentMember) {
          clearInterval(waitForMemberstack);
          await updateNavButtons();
    
          // Listen for auth changes (more reliable than polling)
          memberstack.onAuthChange((member) => {
            if (member) {
              window.location.href = dashboardUrl; // Redirect after login
            }
          });
        }
        if (attempts > 20) {
          clearInterval(waitForMemberstack);
          console.error("Memberstack failed to load after 6 seconds.");
        }
      }, 300);
    
      // Rest of your existing code for buttons...
      async function updateNavButtons() {
        const { login, signup, logout } = getLinks();
        const member = await memberstack.getCurrentMember();
    
        if (member?.data) {
          hide(login);
          hide(signup);
          show(logout);
        } else {
          show(login);
          show(signup);
          hide(logout);
        }
      }
    
      // Handle button clicks
      document.addEventListener("click", async function (e) {
        const link = e.target.closest("a");
        if (!link) return;
    
        const href = link.href;
        const ms = window.$memberstackDom;
        if (!ms) return;
    
        // LOGIN: Use memberstack.loginMemberEmailPassword() with redirect
        if (href.includes("/login")) {
          e.preventDefault();
          try {
            await ms.loginMemberEmailPassword({
              email: "user@example.com", // Replace with dynamic input if needed
              password: "userpassword",  // Replace with dynamic input if needed
            }, { redirectUrl: dashboardUrl }); // Explicit redirect
          } catch (error) {
            console.error("Login error:", error);
          }
        }
    
        // LOGOUT
        if (href.includes("/logout")) {
          e.preventDefault();
          await ms.logout({ redirectUrl: "/" }); // Explicit redirect after logout
        }
      });
    });
    Copy


    Key Fixes:

    1. Explicit redirectUrl in loginMemberEmailPassword(): Memberstack will now automatically redirect to /affiliate-dashboard after a successful login, bypassing Dorik’s URL interference.

    2. onAuthChange for Reliable Redirects: Uses Memberstack’s built-in auth listener instead of polling, avoiding race conditions with Dorik.

    3. Cookie Handling: The useCookies: true initialization ensures authentication works across domains (critical for Dorik).

    4. Avoid window.location.href Post-Login: Dorik’s client-side routing likely intercepts these redirects. Let Memberstack handle it via the redirectUrl parameter.



    Debugging Steps if It Still Fails:

    1. Check Dorik’s Console for Errors: Dorik might be throwing errors that block Memberstack. Look for:

      • Refused to set unsafe header (cookie blocking)
      • Uncaught TypeError (Dorik overriding window.location)
    2. Test Without Dorik’s Routing:

      • Temporarily disable Dorik’s client-side routing (if possible) and test if the redirect works.
      • Or host the login/logout buttons outside Dorik’s app (e.g., in a subdomain or static HTML file).
    3. Verify Memberstack’s Redirect Settings: Double-check in your Memberstack Dashboard that:

      • /affiliate-dashboard is set as the default login redirect for all plans.
      • There are no plan-specific redirects overriding this.
    4. Fallback: Use ms.redirectToMemberStack(): If Dorik absolutely refuses to cooperate, you can use Memberstack’s direct redirect:

      if (member) {
        ms.redirectToMemberStack(dashboardUrl); // Force redirect via Memberstack
      }
      Copy


    Why This Works:

    • Memberstack’s redirectUrl parameter ensures the redirect happens before Dorik’s router interferes.
    • The onAuthChange listener avoids race conditions by reacting to auth state changes in real-time.
    • Explicit cookie handling (useCookies: truebypasses Dorik’s security restrictions.


    Next Steps:

    1. Replace YOUR_PUBLIC_KEY and YOUR_APP_ID in the code.
    2. Test the updated script in Dorik’s preview mode or a staging environment.
    3. If issues persist, share the browser console errors again—I’ll help you debug further!

    Would you like me to clarify any part of the fix? 😊

    0

Please sign in to leave a comment.

Sitemap