Skip to main content

How to Change User’s Password in Laravel

 

While working on the application, you may want to quickly change the password of a user for some reasons and you don’t have access to the database. Plus, you don’t want to go for forgot password flow. In this article, I show you how to change a user’s password in Laravel without accessing the database.

When it comes to Laravel, it ships with a default ‘users’ table in the database. This table is responsible for storing the user details including credentials like email and password.

Laravel uses their own Hashing to encrypt the password. For changing the password you need to use the same hashing techniques for encryption then only it works with Laravel authentication.

Change User’s Password Using Command Line

There are several features Laravel included in its core. Upon installing Laravel, we get all these features automatically. One of them is Laravel Tinker, a REPL powered by the PsySH package. Laravel Tinker allows us to interact with Laravel database, jobs, events and more.

We can use the Tinker to change the password of a user in Laravel. All we need to know is the email address of a user. So for getting started, open the terminal in your project root directory.

Enter into the Tinker environment by running the command:

php artisan tinker

tinker

Once you enter into the Tinker environment, you’ll have the control over Eloquent ORM. Using the ORM, you can easily change the user’s password. Let’s say the user’s email is ‘admin@laravel.com’ which password needs to change. At first, fetch this user by a statement:

$user = App\User::where('email', 'admin@laravel.com')->first();

get-user

Next, on the user object set the password using the Hash::make() method. For instance, set the password as ‘123456’. So the next statement is:

$user->password = Hash::make('123456');

set-password

Finally, call save() method of Eloquent ORM as follow:

$user->save();

save user

The above 3 commands will change the password of a specified user.

Change User’s Password in Laravel Using Route

You can also change the password through the route. However, it is not recommended. I am just writing about it as this is also the way to change the password. In the route’s callback function, use the same code as we used in the Tinker above. Both would do the exact job. Let’s declare a route ‘changepassword’ and pass the code in the callback function as shown below.

routes/web.php

1
2
3
4
5
6
7
8
9
<?php
Route::get('changepassword', function() {
    $user = App\User::where('email', 'admin@laravel.com')->first();
    $user->password = Hash::make('123456');
    $user->save();
 
    echo 'Password changed successfully.';
});
?>

Now, run the URL YOUR_DOMAIN_URL/changepassword in the browser. It will call the route and change the user’s password. The developer must remove this route once the password is changed.

Comments

Popular posts from this blog

Sample VS code setting

  When you are a software developer, you need to configure your IDE working with most convinience for your working, bellow is a sample code snippet to config your IDE {   "diffEditor.ignoreTrimWhitespace" : false ,   "javascript.updateImportsOnFileMove.enabled" : "always" ,   "[typescriptreact]" : {       "editor.defaultFormatter" : "esbenp.prettier-vscode"   },   "editor.formatOnPaste" : true ,   "workbench.settings.applyToAllProfiles" : [],   "editor.tabSize" : 2 ,   "redhat.telemetry.enabled" : true ,   "editor.codeActionsOnSave" : {       },   // "editor.codeActionsOnSave": {   //   "source.fixAll": "explicit",   //   "source.fixAll.eslint": "explicit",   //   "source.organizeImports": "explicit",   //   "source.sortMembers": "explicit",   //   "javascript.showUnused": "...

Docker Compose: Node.js Express and MongoDB example

  Docker   provides lightweight containers to run services in isolation from our infrastructure so we can deliver software quickly. In this tutorial, I will show you how to dockerize Nodejs Express and MongoDB example using   Docker Compose .