Detect if admin is logged in?

How can I detect is the admin logged in and put it as if to my twig file like:

{% if admin.login %}
   // do something here
{% endif %}

{% if grav.user.title == “Administrator” %}
{{ “I ain’t sayin she a gold-digger” }}
{% endif %}

Does this work? (assuming you set your title to Administrator when you made your admin account, which you can see in the admin panel in your user info.)

Or something like this?

{% if grav.user.access.admin %}
{{ "Hello Admin" }}
{% endif %}
{% if grav.user.access.admin.super == true %}
{{ "Hello Admin" }}
{% endif %}

@davidalanbruce thanks for ideas but none of these works. I also tried to follow login page from admin plugin you can find plugins/admin/themes/grav/templates/login.html.twigand reuse

{% if notAuthorized %}
// do something
{% else %}
// do something else
{% endif %}

but these don’t work as well :confused:

There were a couple restrictions in the above for me.

  1. I had to be logged in (on the website) - The admin panel seemed to be separate, so I enabled a login for the page.

  2. I had to clear the cache, I’ve just realised that could be problematic if you viewed the page not logged in, then log in (as the changes wouldn’t show unless the cache was cleared)

You’d have to find a better way or get around the above somehow. I’m not very familiar with how the caching works. Sorry I couldn’t help more.

Edit: You could try cache_enable: false in the header frontmatter, which worked for me, at the cost of caching the page?

According to the source of GRAV the generated basename of the session is always the same, and the only extension that an administrator login gets is ‘-admin’. So this works:

$adminCookie = session_name() . '-admin';

if (isset($_COOKIE[$adminCookie]) === false) {

Docs here on User Object