<p>Yes, that's expected behavior.  <code>currentUser</code> will not exist if there is not a currently logged in user.</p>

<pre><code>{% if currentUser.isInGroup('verified') is defined %}
</code></pre>

<p>That's one workaround for it.</p>

<p>Another would be:</p>

<pre><code>{%if currentUser is defined and currentUser.isInGroup('verified') %}
</code></pre>

<p>which to me reads a little better even though it's more verbose.</p>

CC BY-SA 3.0
2015-11-25T00:28:24.413Z
Perfect, good to know. Using `currentUser` on it's own (without `is defined`) is perfectly fine though, right?
Rob
CC BY-SA 3.0
2015-11-25T00:31:52.553Z
Yes, you can just do: `{%if currentUser and currentUser.isInGroup('verified') %}`
andrew.welch
CC BY-SA 3.0
2015-11-25T00:34:40.237Z
Nice one, thanks @khalwat ... that feels a little cleaner
Rob
CC BY-SA 3.0
2015-11-25T00:23:27.357Z
2015-11-25T00:23:27.357Z
Brad Bell
<p>I couldn't find this anywhere else, I'm looking up if the <code>currentUser.isInGroup('verified')</code> and if not, display another link. Works fine when logged in but throws a Template and Runtime error when using the <code>{{logoutUrl}}</code> or viewing any page when not logged in.</p>

<p>I expected this would work the same as <code>currentUser</code> by itself, which throws no errors. But obviously there's something I'm missing. Should I be using something like <a href="http://buildwithcraft.com/docs/templating/craft.session" rel="nofollow noreferrer"><code>craft.session</code></a> in combination with the <code>isInGroup</code>? Or some other method of detection?</p>

<p><a href="https://i.sstatic.net/GMPyi.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/GMPyi.png" alt="enter image description here"></a>
<a href="https://i.sstatic.net/6r8U4.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/6r8U4.png" alt="enter image description here"></a></p>

<p><strong>Update</strong></p>

<pre><code>{% if currentUser.isInGroup('verified') is defined %}
</code></pre>

<p>Seems to get rid of the errors. So a couple of questions:</p>

<ul>
<li>Is this expected behaviour?

<ul>
<li>I'm yet to wrap my head around the <code>|length</code> <code>is defined</code> etc methods of checking, and when to use them. I'll have to <a href="https://craftcms.stackexchange.com/questions/824/test-for-empty-field/836#836">go over this post a few more times</a>!</li>
</ul></li>
</ul>

<templating><users><user-groups><group><current-user>
currentUser: Impossible to invoke a method ("isInGroup") on a null variable
# currentUser: Impossible to invoke a method ("isInGroup") on a null variable

(Asked by: username_0 on 2015-11-25)

I couldn't find this anywhere else, I'm looking up if the currentUser.isInGroup('verified') and if not, display another link. Works fine when logged in but throws a Template and Runtime error when using the {{logoutUrl}} or viewing any page when not logged in.
I expected this would work the same as currentUser by itself, which throws no errors. But obviously there's something I'm missing. Should I be using something like craft.session in combination with the isInGroup? Or some other method of detection?


Update
{% if currentUser.isInGroup('verified') is defined %}

Seems to get rid of the errors. So a couple of questions:

Is this expected behaviour?


I'm yet to wrap my head around the |length is defined etc methods of checking, and when to use them. I'll have to go over this post a few more times!

**Tags:** templating, users, user-groups, group, current-user


---

## 1 answers

### Answer by username_1 on 2015-11-25. Score: 1 | Accepted answer

Yes, that's expected behavior.  currentUser will not exist if there is not a currently logged in user.
{% if currentUser.isInGroup('verified') is defined %}

That's one workaround for it.
Another would be:
{%if currentUser is defined and currentUser.isInGroup('verified') %}

which to me reads a little better even though it's more verbose.

> **Comments:**
> **username_0**: Perfect, good to know. Using `currentUser` on it's own (without `is defined`) is perfectly fine though, right?  
> **username_2**: Yes, you can just do: `{%if currentUser and currentUser.isInGroup('verified') %}`  
> **username_0**: Nice one, thanks @khalwat ... that feels a little cleaner  

---

