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?

  • My error - I corrected the code above to show that, indeed, I do have craft()->userSession->login($username, $password)
    – buck1112
    Oct 4, 2018 at 15:46
  • This is in Craft 2, right? How exactly are you triggering the controller action?
    – Ben Croker
    Oct 5, 2018 at 18:34
  • Yes it is, thanks. I am triggering the controller action via the input type="hidden" name="action" value=myPluginName/myClassName/myFunctionName.
    – buck1112
    Oct 5, 2018 at 19:29
  • 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
    Oct 5, 2018 at 19:33
  • To clarify my last post, by "working," I mean that the plugin is logging me in as the test user.
    – buck1112
    Oct 5, 2018 at 19:33

1 Answer 1


If you want to login a user, you need to use the login function

craft()->userSession->login($loginName, $password, $rememberMe))
  • Is the $rememberMe parameter optional, and is there an expiration on it?
    – buck1112
    Oct 4, 2018 at 15:47
  • @Robin How to do this in Craft 3, please?
    – Ra3d K
    Mar 15, 2022 at 6:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.