This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

mathieu.m9546
mathieu.m9546

Hello,

Is it possible to have a backend link on the frontend, only visible if the user has a valid active session in backend ?

Many thanks.

JSokol
JSokol

Try to determine if the user is logged in on the backend with:

use BackendAuth;
$user = BackendAuth::getUser();
mathieu.m9546
mathieu.m9546

Thanks Jsokol. I'm sorry if my question seems stupid, but i'm new to OctoberCMS and trying to understand how everything goes together. How do I integrate your code ?

I've tried to paste it in the "Code" part of the editor and recover the variable result with a "if" statement, but then the page throws me an error.

Thanks.

JSokol
JSokol

No worries. Let's say you want to use in a component. Example of usage below:

<?php namespace Default\PathTo\Components;
use Cms\Classes\ComponentBase;
use BackendAuth;

class AskQuestion extends ComponentBase
{
    public $loggedIn;

    public function componentDetails()
    {
        return [
            'name'        => 'Test Component',
            'description' => 'Blaaaah'
        ];
    }

    public function defineProperties()
    {
        return [];
    }

    public function onRun() {
        // by default we can assume that users are not logged in
        $this->loggedIn = false;

        // end then if getUser returns other value than NULL then our user is logged in
        if (!empty(BackendAuth::getUser())) {
            $this->loggedIn = true;
        }
    }
}

Then in the default theme you can just do something like this:

{% if __SELF__.loggedIn %}
    USER LOGGED IN
{% else %}
    WELCOME ANONYMOUS!
{% endif %}

Good luck!

1-4 of 4

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.