Redirecting the logged in users to a specific page Answered

Post author
Lucas Suárez

Hi you all!

I have a page that is only visible for logged in users. When users access this page and are not logged in, they are redirected to login page. In this moment, when they login, instead of being redirected to the previous page, they are redirected to the dashboard, that is the page set by default to be redirected on login. How can I solve that? I have some pages that I need the users come back after login, and just in this cases, overwrite the default redirect setting.

Comments

11 comments

  • Comment author
    Tyler Bell

    https://webflow.com/made-in-webflow/website/redirect-to-attempted-page

    0
  • Comment author
    Lucas Suárez

    Thaaaaank you!

    Quick question:

    My success page is the dashboard, so, after pay the user is redirected to this dashboard.

    If I put this code in the dashboard page:

    <script>
    // redirects back to the page you were just on
    window.location = localStorage.getItem('locat');
    </script>

    The script will run only if there is an URL in the local storage, right?

    0
  • Comment author
    Tyler Bell

    That’s correct.

    0
  • Comment author
    Lucas Suárez

    I’m thinking that this script will also affect in undesired cases, for example:

    1. User accesses /product-page (URL is saved in local storage).
    2. The user clicks on “Buy” and the singup process begins.
    3. After paying, the user is redirected to the success page.
    4. At that point, since the user has saved /product-page in their local storage, they will be returned to the product page.

    In my case, I only need this to work with a couple of URLs, does it make sense to make the script work in reverse? That is, instead of having a list of excluded URLs, to have a list with the URLs that I want to save in local storage.

    Like this:

    <script>
    
    //List of paths to include
    var includePaths = ['/alta-comunidad', '/community'];
    
    //Get the current path
    var path = window.location.pathname;
    
    //Set the location in localstorage only if the current path is in the list of included paths.
    if (includePaths.includes(path)) {
    	localStorage.setItem('locat', location.href);
    }
    
    </script>

    Does it make sense or have I done something wrong? 😅

    0
  • Comment author
    Tyler Bell

    cc Julian Galluzzo

    I believe Julian created the original code for that so he’ll be best to answer that question 🙂 Julian, if your busy, just let me know and I’ll give an answer a try.

    0
  • Comment author
    Julian Galluzzo

    Checking now!

    Lucas Suárez check this out - I made this one yesterday for someone else and i think it captures your situation perfectly.

    <script>
      // Exclude paths from running the script
      var excludedPaths = ['/success'];
    
      // Get the current path
      var currentPath = window.location.pathname;
    
      // Check if the current path is in the excluded paths
      var isExcluded = excludedPaths.includes(currentPath);
    
      // Set the location in localStorage only if the current path is not in the list of excluded paths
      if (!isExcluded) {
        // Check if the current path contains '/projects/'
        var isInProjects = currentPath.includes('/projects/');
    
        if (isInProjects) {
          // Set the location in localStorage if the current path contains '/projects/'
          localStorage.setItem('locat', location.href);
        } else {
          // Clear the 'locat' item from localStorage if the current path does not contain '/projects/'
          localStorage.removeItem('locat');
        }
      }
    </script>
    0
  • Comment author
    Julian Galluzzo

    No problem!!

    0
  • Comment author
    Lucas Suárez

    I’ll check it and let you know. Thanks Julian!

    0
  • Comment author
    Heather B

    Hey memberstack team! sorry if this is a noob question - how can I redirect signed in users to the homepage if they somehow land on the sign-up and log-in pages? The screenshot below didn't work - thanks

    0
  • Comment author
    A J

    Hey Heather B, nothing is a noob question.

    I hope this will solve your use-case perfectly:
    https://docs.memberstack.com/hc/en-us/articles/13156905423899--Code-Redirect-Logged-in-Members

    0
  • Comment author
    Heather B

    perfect thank you 😊

    I am now trying to do a similar redirect for a sign up page that allows users to join another plan e.g. an author sign up page. If a member who is already an author lands on this page, I want them to be redirected, however the code I generated (which is like the script in your link with more checks) is not working, do you have any ideas please?

    0

Please sign in to leave a comment.