• Resources
  • Add Member Logged In/Out Class - Simple Guide No Extension

Add Member Logged In/Out Class Introduction

In Flarum, optimizing user engagement is essential for building a vibrant community. A powerful approach to achieve this is by using conditional visibility for different elements based on users' login status. This guide presents a method for assigning global "logged-in" and "logged-out" classes to the <body> element, allowing forum administrators to control the visibility of various elements for both logged-in users and guests.

Why Is It Needed?

User Engagement: Tailoring content and features for logged-in and logged-out users enhances interaction within the community.
Visual Clarity: By managing element visibility based on login status, the interface remains organized and user-friendly.
Customizability: This method offers flexibility for forum administrators to implement design changes without altering the core Flarum codebase.

Existing Plugins: There are similar functionalities available in the Flarum community, such as Member Add Logged-in Class to Body Tag, which is no longer supported. These plugins previously allowed users to add a class to the body tag based on their login status, enabling administrators to customize user experiences visually. However, this particular plugin is outdated, making our approach a more reliable option.

Functions

Global Class Assignment: The script checks the user's login status and assigns either "logged-in" or "logged-out" classes to the <body> element.
Conditional Display: Using CSS, it allows you to manage the visibility of any element based on the assigned class.

Usage Example

The following code snippets can be used to implement this functionality.

JavaScript Code

Add the JavaScript code to the 'Custom Footer' section on the Admin Panel in Appearance.

<script>
document.addEventListener("DOMContentLoaded", function() {
    setTimeout(function() {
        const isLoggedIn = app.session.user !== null;
        if (!isLoggedIn) {
            document.body.classList.add('logged-out');
        } else {
            document.body.classList.add('logged-in');
        }
    }, 1); // 1 ms
});
</script>

CSS Code

Add the CSS code to the 'Custom CSS' section on the admin panel in the Appearance.

header.Hero.WelcomeHero {display: none;} /* Hide default */
body.logged-out header.Hero.WelcomeHero {display: block;} /* Show visitors */

/* Example: Control other elements with CSS */
body.logged-in .some-element {display: none;} /* Hide members */
body.logged-out .some-element {display: block;} /* Show visitors */

How It Works

JavaScript: When the page loads, the DOMContentLoaded event is triggered. After the content is fully loaded, it waits for 1 millisecond to check the user's login status. If the user is not logged in, it adds the logged-out class; if logged in, it adds the logged-in class to the body.

CSS: You can control the visibility of any element based on the user's login status by using these classes. For instance, when the logged-in class is present, specific content can be hidden, or with the logged-out class, other content can be displayed.

Conclusion

This method is an effective way to enhance the user experience on your Flarum forum. By managing content based on whether users are logged in or not, you can provide tailored experiences for both visitors and members. Feel free to add this code to your forum and create a structure that meets the needs of both users and administrators.

Feedback

Don't hesitate to implement this code snippet in your Flarum forum and share your experiences or improvements! Your feedback and enhancements will contribute to our community's knowledge base.

I'm looking forward to your suggestions and thoughts on the code. Also, I don't know how to create an extension. If someone is able to turn this into an extension, I'm sure there will be people interested in using it. Your feedback is very valuable to me!

The feature added in this discussion and the extension that adds BBCode functionality:
https://discuss.flarum.org/d/36589-add-member-logged-inout-bbcode-and-class

Lastly, if you need a footer for your Flarum forum, you can access the code I shared, which is fully compatible and responsive without the need for any extensions, from this topic: https://discuss.flarum.org/d/36061-responsive-footer-for-flarum-fully-compatible

JasperVriends i updated the JavaScript code as you suggested, and it works. Thank you for your feedback. Knowing that you are experienced with Flarum, I implemented it, but I didn't quite understand the difference between the two methods.

Also, since my native language is not English, I might have misunderstood your comment. If you meant that this JavaScript code is not needed at all, could you please clarify? Thank you!

    Subarist Thank you! I'm glad you found the tips useful. If you decide to use this method, please let me know if everything works smoothly or if you encounter any issues. Your feedback would be really helpful!

      huseyinfiliz I learned how to achieve this in blade using @if (!Auth::check()) on another laravel-based forum.
      I hadn't thought about applying JS on Flarum to get a similar effect, but I'll find some time to give it a try.

        Subarist I believe the same logic can be applied in Flarum, but unfortunately, I donโ€™t have the knowledge to implement it. If any work is done on this, I can either add it directly to this topic or as a link

        Hey huseyinfiliz , no worries! The difference is that the app variable is defined by the Flarum page and contains the currents user state, which is saved in the session object. null means that is has not been set. So if app.session.user is null you know that there's no user signed in. If you use !== null, then you know that the user is signed in ๐Ÿ™‚

        If you meant that this JavaScript code is not needed at all, could you please clarify?

        What I meant was the following: in the previous version you were checking if a classname exists on the page, but that isn't really relyable since Flarum could change that classname in the future. By using the JavaScript user object, you are sure it will work unless the dev team announces something differently while changing a classname in the interface wouldn't be announced ๐Ÿ™‚

          huseyinfiliz
          OK, it's working great.
          I applied this method to the homepage prompt, and itโ€™s working as expected.
          Now I feel like there are many ideas I can implement. Thanks again!

          logged-out

          logged-in

            Subarist Great, the only limit to your ideas is your imagination! I'm glad I could help. Wishing you success with your new ideas as well! Thanks again!

            I think the CSS selector for logged-in and logged-out users should be a global selector like @phone, and it should be used by all CMS platforms. I hope this becomes a common practice across the internet in the future. Because itโ€™s truly something very useful and practical.

            a month later

            Subarist i wanted to let you know about this feature since I know you're interested in it. I have taken over the maintenance and development of both this feature and the extension that adds BBCode for members and guests.

            I'm currently using it on my own forum, and it's working perfectly. If you'd like to try it out, you can check out the extension here:

            https://discuss.flarum.org/d/36589-add-member-logged-inout-bbcode-and-class