I am trying to create a single sign on module for my company within Magento. I've set up a connection to the OAuth guarded server and managed to log in on the authentication server.

What I would like to do now is create an admin user in Magento if one doesn't exist with the retrieved username. If there is an administrator user with the given username then I want to log them in.

Currently I'm having trouble finding the administraot user object by username. I've found the following code:

/** @var \Magento\User\Model\User $user */
$user = $this->_objectManager->get('Magento\User\Model\User')->loadByUsername('username');

Which returns an object regardless of the username I enter but the object doesn't seem to contain any usable administrator data.

Concretely my question is: how do I retrieve the user/admin model by its username and how do I verify if that model actually contains a user/admin object which I use to log in.

1 Answer 1


You can check if you are getting userId from the object

    echo "User Found";
} else{
    echo "Not Found";
  • Thank you for the reply. Is there any documentation available on the user model or did you just look this up in the user class? :) May 22, 2017 at 12:23
  • No this is just year of experience in coding dint refer the docs :)
    – Priyank
    May 22, 2017 at 12:26
  • Thanks, I'll go browse trough the vendor directory some more to find out some more about the Magento code :D If you happen to know how to create and log in the administrator user I'm open for recommendations :) May 22, 2017 at 12:31
  • Ya try it out. Give your efforts and If you face any problems post a new question.
    – Priyank
    May 22, 2017 at 12:33
  • I managed to create the user without any problems. The only problem I have right now is the sign on. I posted a new question on that. magento.stackexchange.com/questions/175549/… Thank you for the help on creating the user :) May 22, 2017 at 14:48

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.