Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Robert Wilchek 17 posts 107 karma points
    Aug 09, 2018 @ 21:36
    Robert Wilchek
    0

    How to redirect when user is already logged on

    Hi, I have a need to do something that I have not figured out yet. I have a site that once you log in I redirect you to a completely different set of pages with a different layout page. This works just fine since I set the layout page and redirect the user after the user logs in and I handle that log in in my logincontroller. But..

    if the user goes off the site and returns back, they are still logged in behind the scenes and go to the wrong version of the site. My question is where can I detect this and redirect them to the right pages based on the logged in user information. I know how do everything except "WHERE"...

    I.E. is there a controller that I can hook? Do I have some way of adding a Global Controller that intercepts the requests so I can redirect? I tried redirecting from my main layout page, but I get into an infinite loop redirecting to the same page essentially since it still finishes the rendering of my main page.

    I also do NOT have a custom controller for basic web site pages, I rely solely on Umbraco for that. I am hoping there is a hook somewhere in umbraco that I can add some code to detect this and then redirect.

    Thank you in advance!

    Robert

  • Greg Hluska 25 posts 133 karma points
    Aug 10, 2018 @ 00:24
    Greg Hluska
    0

    Could you try a helper function like .MemberIsLoggedOn() and do the redirect there if it returns true?

    Check out the example and the documentation:

    https://our.umbraco.com/documentation/Reference/Querying/UmbracoHelper/

  • Robert Wilchek 17 posts 107 karma points
    Aug 10, 2018 @ 01:07
    Robert Wilchek
    0

    The problem is I already know the member is logged on, the real issue is intercepting Umbraco default behaviour and overriding the redirect from there. At least that is my current line of reasoning.

    I know via a multitude of ways that the user is logged on, what role they have, and where I want them to go, what I don't know is "WHEN" and "WHERE" to make this determination that would work.

    Thank you for your feedback, let me know if I am missing the point.

    Robert

  • Greg Hluska 25 posts 133 karma points
    Aug 10, 2018 @ 02:27
    Greg Hluska
    0

    Honestly bud, you're not missing the point at all. You've got this. Good work on building such a good understanding of this. A more plausible answer is that I'm doing a crap job of explaining myself. :)

    Check this out:

    @if(Umbraco.MemberIsLoggedOn()){ Logged in }

    Let's look at that function from the inside out.

    • Umbraco.MemberIsLoggedIn is a boolean. It will return whether the statement is true or false.
    • Moving out, you have an if statement.

    That whole statement says "If a user is logged in, print Logged in."

    Now though, printing 'Logged in' is all fine and good, but it isn't what you want to do. Your goal is to do a redirect.

    Since you're catching this on the front end, Javascript would be one way to rock a redirect, but we're hep, with it developers, so we want to think this through a little more.

    Do a large percentage of your logged in visitors keep Javascript turned off? If not, let's talk about browsers? Do you have to support anything like IE6?

    If pure .js works for you, read about a Javascript method called location.replace(). It is similar to an http redirect in that it will send the user to a new page, but it won't keep the page it occurs on in history. That's because it replaces the page you're on with the new one.

    https://developer.mozilla.org/en-US/docs/Web/API/Location/replace

    Edit - Forgive the crappy formatting on the code snippet. I've really got to spend some time figuring out this forum. I'll get better...

    Edit2 - Pure .js likely isn't the best solution, but I don't want to get into Javascript fallbacks until we're on roughly the same page.

  • Robert Wilchek 17 posts 107 karma points
    Aug 10, 2018 @ 13:15
    Robert Wilchek
    0

    Thanks for the prompt reply. So I wired up a document ready function in my master layout page that makes an ajax call to my login controller. It make the determination that I need to redirect and the location.replace works just fine.

    To make it work thru document ready though is going to be uber challenging since i have to essentially track all my pages somehow to determine when I need to redirect and when I don't. But, if this is the best way, then this is the best way...

    Alternatively it would be really nice if there was a simple way for me to catch when Umbraco makes the determination that the user is logged in thru some kind of membership hook?

    Thank you for you suggestion, I will run with this unless something better comes along.

    Robert

  • Greg Hluska 25 posts 133 karma points
    Aug 10, 2018 @ 15:07
    Greg Hluska
    1

    Hi Robert,

    Did you read the documentation and try out that helper function?

    I don't know your project, your code or your requirements, so take this with a grain of salt, but I'm worried that you're designing yourself into one hell of a mess. As your project grows, I worry that your document ready function will evolve into a mess of switch statements where each case corresponds to a different URL and either forces the redirect sequence or not depending on page title.

    As your requirements evolve, this could easily turn into a function where you have to treat every single page differently. Then, heavens help you if your URL structure changes dramatically, or if you have to hand this project off to another developer. I'd also worry about how this will respond to any kind of load or performance degradation. You've designed a solution where you're making a front end call to a controller to see if a user is logged in on every single page! That x 10,000 users is one hell of a load upon a poor little server.

    You also have to consider that your entire implementation will die the moment someone with JavaScript turned off visits. If you have any kind of business or compliance reason to be accessible, you'll find that most people who use screen readers keep JavaScript turned off.

    If I were you, I'd go into a dev environment and mess around with that helper function. Maybe start by just printing 'logged in' or 'logged out' (or 'logged on' or 'logged out' depending on what words you prefer) on a page depending on the status. Or, even just start printing the results of the helper function so you can see the boolean it returns. From there, you have a JavaScript solution, but you should also start thinking about the fallback for people who keep JavaScript turned off. I'm trying not to give you all the answers, but look into meta redirect tags.

  • Harrison 42 posts 254 karma points
    Aug 11, 2018 @ 17:13
    Harrison
    1

    Greg's suggestion for using MemberIsLoggedIn is what you need, but I'd setup a base controller that inherits RenderMvcController to do the check and handle the redirect. Within that base controller you can have a private method that checks the Member's login status, and then redirect if needs be.

    There's a couple of things you'd need to do. The controller would look something like the below

        public class BaseController : RenderMvcController{
                   public BaseController(){
                          //Call private method in 
                   }
    
                   private void ValidateMember(){
                          //Check member status and redirect if logged in
                   }
    
        }
    

    Then within your ApplicationStartUp file, you need to set the default controller type to your new BaseController

    DefaultRenderMvcControllerResolver.Current.SetDefaultControllerType(typeof(BaseController));
    

    The base controller will then be called on every page view, so you can add whatever you need to for a per page view basis.

    Hope this helps!

  • Robert Wilchek 17 posts 107 karma points
    Aug 20, 2018 @ 13:41
    Robert Wilchek
    100

    Nice, This is what I was looking for. I already have a basecontroller and a basesurfacecontroller. The link I was missing was:

    DefaultRenderMvcControllerResolver.Current.SetDefaultControllerType(typeof(BaseController));

    This was the missing piece to my puzzle. I added an index override to my base controller, put my logic in there to redirect, and viola, it is working perfectly!

    Thanks!

    Robert

Please Sign in or register to post replies

Write your reply to:

Draft