<p>If you want to login a user, you need to use the <code>login</code> function</p>

<pre><code>craft()-&gt;userSession-&gt;login($loginName, $password, $rememberMe))
</code></pre>

CC BY-SA 4.0
2018-10-04T15:47:27.243Z
Is the $rememberMe parameter optional, and is there an expiration on it?
buck1112
CC BY-SA 4.0
2022-03-15T06:59:38.560Z
@Robin How to do this in Craft 3, please?
Ra3d K
CC BY-SA 4.0
2018-10-03T21:13:30.373Z
2018-10-03T21:13:30.373Z
Robin Schambach
<p>I'm writing an LDAP authentication plugin, which, if the authentication matches, will log the user into a 'general' username/password, in order to view a protected page.  The 'general' username/password is part of a usergroup with restricted priveleges. </p>

<p>In my plugin, in the controller file, I have this:</p>

<pre><code>craft()-&gt;userSession-&gt;login($username, $password);
</code></pre>

<p>with the $username and $password variables set just above that.  However, the login process does not appear to be happening, as my check in my template is not displaying the username:</p>

<pre><code>{% if currentUser %}
Welcome, {{ currentUser.friendlyName }}!
{% endif %}
</code></pre>

<p>However, when I log on as that 'general' username/password, from the login page, and then refresh my template, it does show that the login for that profile has successfully occurred.  </p>

<p>What may need to be done to effect a successful login, in the plugin?<br>
Thanks!</p>

CC BY-SA 4.0
2018-10-04T15:46:02.720Z
My error - I corrected the code above to show that, indeed, I do have craft()->userSession->login($username, $password)
buck1112
CC BY-SA 4.0
2018-10-05T18:34:08.327Z
This is in Craft 2, right? How exactly are you triggering the controller action?
Ben Croker
CC BY-SA 4.0
2018-10-05T19:29:01.510Z
Yes it is, thanks.  I am triggering the controller action via the input type="hidden" name="action" value=myPluginName/myClassName/myFunctionName.
buck1112
CC BY-SA 4.0
2018-10-05T19:33:17.680Z
The really odd thing is that it is working today, without any changes being made today.  I wonder if it's a caching issue.  One other common 'issue' is that when I re-login (as the same user), I get a "the csrf token could not be verified".  When I hit the <enter> key with the url, the form loads correctly (without the error message).  Beyond that little quirk, it seems to be working ok.  I'd still like to trap that error somehow, though.
buck1112
CC BY-SA 4.0
2018-10-05T19:33:48.503Z
To clarify my last post, by "working," I mean that the plugin is logging me in as the test user.
buck1112
<plugin-development><login><craft2>
Programmatically Login From Plugin
# Programmatically Login From Plugin

(Asked by: username_0 on 2018-10-03)

I'm writing an LDAP authentication plugin, which, if the authentication matches, will log the user into a 'general' username/password, in order to view a protected page.  The 'general' username/password is part of a usergroup with restricted priveleges. 
In my plugin, in the controller file, I have this:
craft()->userSession->login($username, $password);

with the $username and $password variables set just above that.  However, the login process does not appear to be happening, as my check in my template is not displaying the username:
{% if currentUser %}
Welcome, {{ currentUser.friendlyName }}!
{% endif %}

However, when I log on as that 'general' username/password, from the login page, and then refresh my template, it does show that the login for that profile has successfully occurred.  
What may need to be done to effect a successful login, in the plugin?
Thanks!

**Tags:** plugin-development, login, craft2

> **Comments:**
> **username_0**: My error - I corrected the code above to show that, indeed, I do have craft()->userSession->login($username, $password)  
> **username_1**: This is in Craft 2, right? How exactly are you triggering the controller action?  
> **username_0**: Yes it is, thanks.  I am triggering the controller action via the input type="hidden" name="action" value=myPluginName/myClassName/myFunctionName.  
> **username_0**: The really odd thing is that it is working today, without any changes being made today.  I wonder if it's a caching issue.  One other common 'issue' is that when I re-login (as the same user), I get a "the csrf token could not be verified".  When I hit the <enter> key with the url, the form loads correctly (without the error message).  Beyond that little quirk, it seems to be working ok.  I'd still like to trap that error somehow, though.  
> **username_0**: To clarify my last post, by "working," I mean that the plugin is logging me in as the test user.  

---

## 1 answers

### Answer by username_2 on 2018-10-03. Score: 2

If you want to login a user, you need to use the login function
craft()->userSession->login($loginName, $password, $rememberMe))

> **Comments:**
> **username_0**: Is the $rememberMe parameter optional, and is there an expiration on it?  
> **username_3**: @Robin How to do this in Craft 3, please?  

---

