cancel
Showing results for 
Search instead for 
Did you mean: 

Programatically created user - wrong password when logging in?

Programatically created user - wrong password when logging in?

I have to make a cron job that once a day creates users from a file on the server. The script works fine, but, the users it generates can't log in (only local for now, not live).

Here is the code:

$customerFactory = $objectManager->get('\Magento\Customer\Model\CustomerFactory');
$customer = $customerFactory->create();
$customer->setWebsiteId($websiteId);
$customer->setEmail($email);
$customer->setFirstname($firstName);
$customer->setLastname($lastName);
$customer->setPassword('Password123');

$customer->save();

Any ideas what I'm doing wrong? When trying to log in with user created with this, it says that the password is wrong.

 

The user is there tho, and in the customer_entity table, there is a user added, and has password_hash set. It just seems that its wrong somehow.

 

PS I know not to use OM like this. I just wanted to test if I can even make users in code like this.

Any help is more than welcome. Thanks.

4 REPLIES 4

Re: Programatically created user - wrong password when logging in?

Hi @blaz_p

 

ok - i understand the problem you are facing , below i am adding the code i have tested it , it works and i am able to logged in on frontend as well - so try below code  : 

 

public function execute()
    {
		$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
 
$storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');
 
$state = $objectManager->get('\Magento\Framework\App\State');
 
$state->setAreaCode('frontend'); // comment this line if you see area code is already set error
 
// Customer Factory to Create Customer
 
$customerFactory = $objectManager->get('\Magento\Customer\Model\CustomerFactory');
 
$websiteId = $storeManager->getWebsite()->getWebsiteId();
 
$store = $storeManager->getStore();  // Get Store ID
 
$storeId = $store->getStoreId();
 
 
// Instantiate object (this is the most important part)
 
$customer = $customerFactory->create();
 
$customer->setWebsiteId($websiteId);
 
$customer->setEmail("manthan@ktpl.com");
 
$customer->setFirstname("John");
 
$customer->setLastname("Doe");
 
$customer->setPassword("manthan@1234");
 
$customer->save();
 
echo 'Create customer successfully'.$customer->getId();
		echo "Goes here";
		exit;   
 }

As you mention , that it is just for testing so i also have taken ObjectManager but its not preferred way - so when you are ready for production code just use factory method instead of ObjectManager.

 

Hope it helps !

 

 

if issue solved,Click Kudos & Accept as Solution

Re: Programatically created user - wrong password when logging in?

Thanks for your reply. Just figured it out. I was somehow setting the wrong store_id and website_id for customer. 

 

This is not valid for me: 

$websiteId = $storeManager->getWebsite()->getWebsiteId();

 

But this is:

$websiteId = $storeManager->getWebsite()->getId();

Re: Programatically created user - wrong password when logging in?

Hi @blaz_p

 

Glad to know that you have figured out the issue , if your issue is solved click on accept as solution so other user gets helped for the same !

 

Happy to help and keep helping Smiley Happy

if issue solved,Click Kudos & Accept as Solution

Re: Programatically created user - wrong password when logging in?

Hi @blaz_p

 

Yes here in your case - below one is also correct to get the WebsiteID !

 

$websiteId = $storeManager->getWebsite()->getId();

 

Hope it helps !

if issue solved,Click Kudos & Accept as Solution