Hello Artisan,
Today our leading topic is laravel reset password manually. In this laravel custom reset password example you will learn how to manually do laravel custom forgot password example. I will explain step by step explain custom password reset laravel.
This post will give you simple example of how to create custom forgot password in laravel with source code so that you can understand easily. So hope this step by step explain laravel custom reset password email.
ou already known that Laravel provide it's own forget password functionality but if you want to create your custom forget password functionality then i will help to creating custom reset password function in php laravel.
We can do it for all version of laravel and can easily create custom forget password with laravel 6, laravel 7 and laravel 8 version. I will create token and will send it via email to create this laravel custom forgot or reset password option.
Step 1: Download Laravel
In this first step we need to get fresh Laravel 8 version application using bellow command, So open your terminal OR command prompt and run bellow command:
composer create-project --prefer-dist laravel/laravel blog
Step 2: Create table
As we are going to create laravel custom reset password email template so we need to create our own custom table for password reset in Laravel. So create it by the following command.
php artisan make:migration create_reset_password_table
And update tha schema like below
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreatePasswordResetsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('password_resets', function (Blueprint $table) {
$table->string('email')->index();
$table->string('token');
$table->timestamp('created_at')->nullable();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('password_resets');
}
}
Read aslo: Laravel 8 Login with Custom Guard Example
Step 3: Create Route
In this laravel 8 reset password example this step is we need to create custom route for forget and reset link. so open your routes/web.php file and add following route.
routes/web.php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Auth\ForgotPasswordController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('forget-password', [ForgotPasswordController::class, 'showForgetPasswordForm'])->name('forget.password.get');
Route::post('forget-password', [ForgotPasswordController::class, 'submitForgetPasswordForm'])->name('forget.password.post');
Route::get('reset-password/{token}', [ForgotPasswordController::class, 'showResetPasswordForm'])->name('reset.password.get');
Route::post('reset-password', [ForgotPasswordController::class, 'submitResetPasswordForm'])->name('reset.password.post');
Step 4: Create Controller
Almost all are set to go. In this step, we need to create ForgotPasswordController and add following code on that file:
app/Http/Controllers/Auth/ForgotPasswordController.php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use DB;
use Carbon\Carbon;
use App\Models\User;
use Mail;
use Hash;
use Illuminate\Support\Str;
class ForgotPasswordController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function showForgetPasswordForm()
{
return view('auth.forgetPassword');
}
/**
* Write code on Method
*
* @return response()
*/
public function submitForgetPasswordForm(Request $request)
{
$request->validate([
'email' => 'required|email|exists:users',
]);
$token = Str::random(64);
DB::table('password_resets')->insert([
'email' => $request->email,
'token' => $token,
'created_at' => Carbon::now()
]);
Mail::send('email.forgetPassword', ['token' => $token], function($message) use($request){
$message->to($request->email);
$message->subject('Reset Password');
});
return back()->with('message', 'We have e-mailed your password reset link!');
}
/**
* Write code on Method
*
* @return response()
*/
public function showResetPasswordForm($token) {
return view('auth.forgetPasswordLink', ['token' => $token]);
}
/**
* Write code on Method
*
* @return response()
*/
public function submitResetPasswordForm(Request $request)
{
$request->validate([
'email' => 'required|email|exists:users',
'password' => 'required|string|min:6|confirmed',
'password_confirmation' => 'required'
]);
$updatePassword = DB::table('password_resets')
->where([
'email' => $request->email,
'token' => $request->token
])
->first();
if(!$updatePassword){
return back()->withInput()->with('error', 'Invalid token!');
}
$user = User::where('email', $request->email)
->update(['password' => Hash::make($request->password)]);
DB::table('password_resets')->where(['email'=> $request->email])->delete();
return redirect('/login')->with('message', 'Your password has been changed!');
}
}
Step 5: Email Setup
in this step, we will add email configuration on env file, because we will send email to reset password link from controller so that user can get their mail and can reset password via that link.
.env
MAIL_DRIVER=smtp
MAIL_HOST=smtp-mail.outlook.com
MAIL_PORT=587
MAIL_USERNAME=your_email_address
MAIL_PASSWORD=your_password
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=your_email_address
Step 6: Create Blade File
We are in the last step of how to create custom reset password functionality in laravel example. Now we need to create blade files for layout, login, register and home page. so let's create one by one files:
resources/views/layout.blade.php
Now paste this code into forgot password blade file.
resources/views/auth/forgetPassword.blade.php
Same copy this below code and paste this into your file:
resources/views/auth/forgetPasswordLink.blade.php
resources/views/email/forgetPassword.blade.php
resources/views/auth/login.blade.php
Now we are ready to run our example so run bellow command to test Laravel custom reset password option:
Now click reset password link and test it.
Read also: Laravel 8.x Custom Login and Registration Example
Hope it can help you.
#laravel #laravel-8x #reset-password #forgot-password #custom