2

I want to generate password reset token in Magento 2.

In magento 1, I was generating token by using below code -

$oldtoken = $cust->getRpToken();
$newResetPasswordLinkToken = Mage::helper('customer')->generateResetPasswordLinkToken();
$cust->changeResetPasswordLinkToken($newResetPasswordLinkToken);`

But not sure how to generate in magento 2.

I have search complete /vendor/magento/module-customer but not able to generate Token.

Edit -

Code for Magento 2 -

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$helper = $objectManager->get('\Magento\Customer\Helper\View');
$newResetPasswordLinkToken = $helper->generateResetPasswordLinkToken();
$cust->changeResetPasswordLinkToken($newResetPasswordLinkToken);
3

You can load the helper \Magento\User\Helper\Data, it's located on vendor/magento/module-user/Helper/Data.php. Then find the function generateResetPasswordLinkToken() on line 50.

======Update on 19:00 16 Jun 2017======

Per user request, I added an example to get token using DI

class XXXX{
    protected $_userHelper;

    /* Other DIs come here */

    public function __construct(
        \Magento\User\Helper\Data $user_helper,
        /* Other DIs come here */
    ) {
        $this->_userHelper= $user_helper;
        /* Other DIs come here */
    }

    /* The function name is up to you */
    public function getToken(){
        return $this->_userHelper->generateResetPasswordLinkToken();
    }
    /* other functions */
}
| improve this answer | |
  • There is no Data.php file at vendor/magento/module-customer/Helper in Magento 2.1.3 – Rahul Jun 16 '17 at 10:06
  • 1
    Sorry, should be vendor/magento/module-user/Helper/Data.php – PY Yick Jun 16 '17 at 10:08
  • please check my updated code for magneto 2 – Rahul Jun 16 '17 at 10:42
  • I recommend you to use Dependency Injection rather than Object Manager – PY Yick Jun 16 '17 at 10:56
  • can u please give example? – Rahul Jun 16 '17 at 11:00
5

Find below

vendor/magento/module-customer/Model/AccountManagement.php

Please find public function initiatePasswordReset($email, $template, $websiteId = null)

you can get it from line 505, as below

$newPasswordToken = $this->mathRandom->getUniqueHash();
$this->changeResetPasswordLinkToken($customer, $newPasswordToken);

======== Reset Password Token for Customer ============

class YOURCLASSNAME {

    protected $_accountmanagement;
    private $_customerRepository;
    private $_mathRandom;

    /* Other DIs come here */

    public function __construct(
        \Magento\Customer\Model\AccountManagement $accountmanagement,
        \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository,
        \Magento\Framework\Math\Random $mathRandom,
        /* Other DIs come here */
    ) {
        $this->_accountmanagement= $accountmanagement;
        $this->_customerRepository= $customerRepository;
        $this->_mathRandom= $mathRandom;
        /* Other DIs come here */
    }

    /* The function name is up to you */
    public function getToken($email, $websiteId){
        $customer = $this->_customerRepository->get($email, $websiteId);

        $newPasswordToken = $this->_mathRandom->getUniqueHash();
        $this->_accountmanagement->changeResetPasswordLinkToken($customer, $newPasswordToken);
    }
    /* other functions */
}
| improve this answer | |
  • please check my updated code for magneto 2. Can u please provide example – Rahul Jun 16 '17 at 10:42
  • @RahulDambare for whom you want to generate token admin user or customer? – Nalin Savaliya Jun 16 '17 at 11:15
  • Customer user. Now my final code is $helper = \Magento\Framework\App\ObjectManager::getInstance()->get('Magento\User\Helper\Data'); $newResetPasswordLinkToken = $helper->generateResetPasswordLinkToken(); echo "\n\nToken - " . $newResetPasswordLinkToken . "\n\n"; $cust->changeResetPasswordLinkToken($newResetPasswordLinkToken); – Rahul Jun 16 '17 at 11:45
  • @RahulDambare Sorry to sound like a beaten drum, but using objectManager is not the right way of doing this. – tim.baker Nov 2 '17 at 15:55
  • use forgot password event dont need to write extra function for rp token – Sarvesh Tiwari Jun 21 '18 at 13:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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