Reset password user Laravel auth using php command

Posted on

This is an article which is written to describe how to reset the password owned by a user exist in a web-page application based on Laravel framework. In this framework, there is a support which is provided by Laravel to manage user for logging in and logging out the web-page application. In other words, it is a certain module for authenticating user for accessing the web-page application. So, this article will provide information on how to reset password user in Laravel auth support using php command.

This article focus on the auth support provided in Laravel 5.3, it can be viewed in the article titled ‘Laravel login page example tutorial for Laravel 5.3‘ which can be visited in this link. After successfully configuring the auth support for the web-page application powered by Laravel 5.3, there are several tables which is used by Laravel 5.3 to save several information relates on users including password of those users.

Below is the structure of the table which is meant to be used for storing user information :

mysql> use research;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+--------------------+
| Tables_in_research |
+--------------------+
| category           |
| main_category      |
| posts              |
| subcategory        |
+--------------------+
4 rows in set (0,00 sec)
mysql>

After executing the command ‘php artisan migrate’ which is solely performed to add several tables to support the authentication feature on the web-application based on the Laravel framework, the result can be seen as follows :

mysql> use research; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A 
Database changed
mysql> show tables;
+--------------------+
| Tables_in_research |
+--------------------+
| category           |
| main_category      |
| migrations         |
| password_resets    |
| posts              |
| subcategory        |
| users              |
+--------------------+
7 rows in set (0,00 sec)
mysql>

There are several new created tables after successfully carried out the command ‘php artisan migrate’. So, the tables which is storing the user and password information used for logging in and logging out the web application is in the newly created table named ‘users’. Below is the description of that table’s structure :

mysql> desc users;
+----------------+------------------+-----+---+-------+--------------+
| Field          | Type             |Null |Key|Default| Extra        |
+----------------+------------------+-----+---+-------+--------------+
| id             | int(10) unsigned | NO  |PRI| NULL  |auto_increment|
| name           | varchar(255)     | NO  |   | NULL  |              |
| email          | varchar(255)     | NO  |UNI| NULL  |              |
| password       | varchar(255)     | NO  |   | NULL  |              |
| remember_token | varchar(100)     | YES |   | NULL  |              |
| created_at     | timestamp        | YES |   | NULL  |              |
| updated_at     | timestamp        | YES |   | NULL  |              |
+----------------+------------------+-----+---+-------+--------------+
7 rows in set (0,01 sec)
mysql>

Supposed at a certain time after successfully registering user via registration page, there is a user which is needed to be changed his or her password manually without having to access the ‘Forgot Password’ feature, is there any method to do such thing ?. Apparently it can be done by changing the value in the password field inside the users table.

By default, based on the creation process of a new user handled by ‘RegistrationController’ class defined in the RegistrationController.php file located in ‘/root-folder-for-laravel-app-project/app/Http/Controller/auth’, there is one important thing which is needed to be properly seen in the following part of line of codes extracted from the file :

/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return User
*/
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}
}

By default, Laravel will use ‘bcrypt’ function to encrypt the password given in the registration process. So, to change the password, we also need to encrypt the plain text password with the ‘bcrypt’ function. Below is how to execute that using a ‘php’ binary command executed in the command line :

user@hostname:~$ php -r "echo password_hash('mypassword', PASSWORD_BCRYPT, ['cost' => 13]) . PHP_EOL;"
$2y$13$VOMEAQ0i7Y.te/Hah7ON7uO0LwMUvH6.OaXQnB2E9GFlMSMGjLISu
user@hostname:~$  

This is retrieved from an article titled ‘Generate a Symfony password hash from the command line exist in this link of Kevin Dunglas’s personal website. Apparently, the one used in Symfony can also be applied in Laravel.

After retrieving the password hash, just execute it in MySQL command console to update the password field value of the record which is associated with the valid user which the password is going to be changed as shown below :

mysql> update users set password='$2y$13$VOMEAQ0i7Y.te/Hah7ON7uO0LwMUvH6.OaXQnB2E9GFlMSMGjLISu' where email='[email protected]';
Query OK, 1 row affected (0,00 sec)
Rows matched: 1  Changed: 1  Warnings: 0
mysql> 

Try to login back to the application using the user which the password has already been changed previously.

One thought on “Reset password user Laravel auth using php command

Leave a Reply