Replace Homepage until logged in



  • When a user goes to mysite.com/ I want them to go to a Welcome (aka welcome.tpl) screen initially -- unless they are logged in. If they are logged in then mysite.com/ would be the nodebb home page (aka home.tpl).

    What's the easiest way to go about this? Modify the '/' route call with an if condition? Or, use the app.load hook? Or maybe a redirect would be easiest?

    Thanks


  • Global Moderator

    @mootzville You could use this in your template I believe, you may have to wrap your usual homepage in the same thing, but without the ! though. Their may be a better way however.

    <!-- IF !loggedIn -->
    
    <!-- ENDIF !loggedIn -->
    


  • @a_5mith Awesome, I'll try that. Thanks!



  • @a_5mith Actually, that's not working for me...I have something like this:

    <!-- IF !loggedIn -->
    <p>You ain't logged in fool!</p>
    <!-- ENDIF !loggedIn -->
    
    <!-- IF loggedIn -->
    <!-- home.tpl goes here -->
    <!-- ENDIF loggedIn -->
    

    Any thoughts?


  • Global Moderator

    @mootzville Depends what template that's in. If that's in home.tpl then you could add ELSEIF to it. So you'd do.

    <!-- IF loggedIn -->
    home.tpl
    <!-- ELSEIF loggedIn -->
    You aint' logged in fool
    <!-- ENDIF loggedIn -->
    

    But again, their may be a better way of doing it, I know you can do it in a script that redirects you to another page. So if you have the page created, you could do it like this: (This goes in a global header widget)

    <script>
    if (!app.uid && ajaxify.currentPage !== 'login' && ajaxify.currentPage !== 'register' && ajaxify.currentPage !== 'welcome') {
     ajaxify.go('welcome');
    }
    </script> 
    

    welcome being the location of relative_url/welcome, but that page will need to be routed. By default that script should usually go to login

    Updated script to avoid the redirect loop.



  • Just to follow up...I ended up just replacing this:

    res.render('home', data);
    

    with this:

    req.user ? res.render('home', data) : res.render('welcome');
    

    in the src/controllers/index.js file.

    This may or may not have side effects...not sure yet...

    I tried to come up with other ways, but this seems the most straight forward. If there is an approach that this can be achieved via hooks I'd love to know...

    Also, from src/routes/index.js can someone explain this:

    function setupPageRoute(router, name, middleware, middlewares, controller) {
    	middlewares = middlewares.concat([middleware.incrementPageViews, middleware.updateLastOnlineTime]);
    
    	router.get(name, middleware.buildHeader, middlewares, controller);
    	router.get('/api' + name, middlewares, controller);
    }
    

    For the router.get() methods, are they like Express' app.get? And if so, I thought unless you pass in next to the method you can't provide more than 1 route? How is it getting the main route plus the api route?


  • Plugin & Theme Dev

    Ouch, this looks way too complex and you took it a whole other level.

    I do this on my site, and its faily simple.

    <!-- IF !loggedIn -->
    <!-- IMPORT welcome.tpl -->
    <!-- ELSE -->
    <!-- IMPORT welcome_loggedin.tpl -->
    <!-- ENDIF !loggedIn -->
    

    This should be in the home.tpl template. You have to create a welcome.tpl and another template (whatever you want to name it is up to you, for example purposes I named the template for when you're logged in welcome_loggedin.tpl) for when you're actually logged in.

    @a_5mith The following template variable does not work for this particular logic: <!-- ELSEIF loggedIn -->


  • Global Moderator

    @trevor Ah right, I was close. 😛



  • @trevor Ok, thanks...yes, your way -- and @a_5mith's way -- is probably better. My solution isn't super complicated...it's actually achieving the same thing more or less and with a single line of code (plus, the template).

    However, I do much prefer handling it within the theme rather than in core nodebb. Makes it easier to maintain over time.

    My reference to the setupPageRoute function was more out of curiosity.

    And, one last question...these conditionals that are used in the templates; where are they defined? Meaning, where do they come from? I hadn't seen them in html before using nodebb.


  • Global Moderator

    @mootzville Think loggedIn is from https://community.nodebb.org/api/config, but I think any true/false can be used as if/else, then begin would do other bits and pieces. Like cats, topics or posts.



  • @a_5mith I see, but also where does this:

    <!-- IF !loggedIn --> come from? Not the loggedIn part, but the if condition itself? What is handling the logic?


  • Global Moderator

    @mootzville Oh right, Psychobunny custom built Templates.js



  • @a_5mith Cool, thanks.


Log in to reply
 


Star

Looks like your connection to NodeBB was lost, please wait while we try to reconnect.