Laravel Custom Login and Registration Tutorial: In this tutorial, we will explain how to create custom authentication login and registration in a Laravel application. Laravel is a top-notch PHP framework that offers numerous packages and plugins to build any type of functionality.
When it comes to the authentication feature, you have the option to install and use the Laravel JetStream package. However, in this tutorial, we will guide you through the traditional method of creating custom authentication in Laravel.
Create Laravel App To begin, create a new Laravel app using Composer. Open your terminal or command prompt and run the following command:
composer create-project --prefer-dist laravel/laravel laravel_demo_app
This command will download the Laravel framework and set up a new Laravel application in a directory named laravel_demo_app.
Connect to the Database Next, you need to configure the database connection for your Laravel app. Open the .env file located in the root directory of your Laravel app. Update the following lines with your database details:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_database_name
DB_USERNAME=your_username
DB_PASSWORD=your_password
Make sure to replace your_database_name, your_username, and your_password with the actual values for your database.
Set Up Auth Controller In Laravel, controllers handle the logic for handling requests and generating responses. We will create a custom controller called CustomAuthController to handle the authentication process. Run the following command in your terminal to generate the controller:
php artisan make:controller CustomAuthController
This command will create a new file named CustomAuthController.php inside the app/Http/Controllers directory. Open the CustomAuthController.php file and replace its content with the following code:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Hash;
use Session;
use App\Models\User;
use Illuminate\Support\Facades\Auth;
class CustomAuthController extends Controller
{
public function index()
{
return view('auth.login');
}
public function customLogin(Request $request)
{
$request->validate([
'email' => 'required',
'password' => 'required',
]);
$credentials = $request->only('email', 'password');
if (Auth::attempt($credentials)) {
return redirect()->intended('dashboard')
->withSuccess('Signed in');
}
return redirect("login")->withSuccess('Login details are not valid');
}
public function registration()
{
return view('auth.registration');
}
public function customRegistration(Request $request)
{
$request->validate([
'name' => 'required',
'email' => 'required|email|unique:users',
'password' => 'required|min:6',
]);
$data = $request->all();
$check = $this->create($data);
return redirect("dashboard")->withSuccess('You have signed-in');
}
public function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password]);
}
public function dashboard()
{
if (Auth::check()) {
return view('dashboard');
}
return redirect("login")->withSuccess('You are not allowed to access');
}
public function signOut()
{
Session::flush();
Auth::logout();
return redirect('login');
}
}
Create Auth Routes Now, we need to define the routes for our custom authentication in the `routes/web.php` file. Open the `routes/web.php` file and add the following code:
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\CustomAuthController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
*/
Route::get('dashboard', [CustomAuthController::class, 'dashboard']);
Route::get('login', [CustomAuthController::class, 'index'])->name('login');
Route::post('custom-login', [CustomAuthController::class, 'customLogin'])->name('login.custom');
Route::get('registration', [CustomAuthController::class, 'registration'])->name('register-user');
Route::post('custom-registration', [CustomAuthController::class, 'customRegistration'])->name('register.custom');
Route::get('signout', [CustomAuthController::class, 'signOut'])->name('signout');
Create Auth Blade View Files To create the login, registration, and dashboard pages, we will use Blade templates. Create a folder named auth inside the resources/views directory. Inside the auth folder, create two files: login.blade.php and registration.blade.php. Open these files and add the respective code provided in your original content.
Run Laravel Development Server Finally, we can run the Laravel development server to test our application. Open your terminal or command prompt and navigate to your Laravel app's root directory. Run the following command:
php artisan serve
This will start the development server, and you can access your Laravel app by visiting http://127.0.0.1:8000/login in your browser.
By following this tutorial, you have learned how to create a custom authentication system in Laravel. You have created a custom controller to handle login, registration, and dashboard functionality, defined the necessary routes, and created the corresponding Blade view files. Feel free to explore further customization and enhance the user authentication experience in your Laravel application.
Categories : Laravel
Tags : Laravel 9 custom login and registration tutorial Custom authentication in Laravel Laravl
order tricor 200mg online cheap buy fenofibrate buy generic fenofibrate for sale
minoxidil price buy cialis 40mg online cheap buy ed pills us
pill acarbose 25mg buy glyburide generic cheap fulvicin
order aspirin 75 mg pill order zovirax for sale imiquad online order
dipyridamole online order buy plendil 5mg online pravachol 10mg over the counter
order dydrogesterone 10mg pill sitagliptin 100mg buy cheap jardiance
how to buy fludrocortisone order rabeprazole 10mg generic purchase imodium generic
buy etodolac pills for sale buy generic etodolac 600 mg purchase cilostazol for sale
buy prasugrel 10mg for sale chlorpromazine for sale online buy tolterodine generic
buy mestinon 60 mg without prescription piroxicam 20mg for sale buy rizatriptan 10mg online cheap
buy ferrous sulfate 100mg sale buy ferrous sulfate 100mg generic buy sotalol 40mg sale
vasotec where to buy casodex tablet duphalac price
buy latanoprost paypal exelon 3mg over the counter buy rivastigmine 6mg sale
premarin 0.625mg over the counter order cabergoline 0.5mg sale sildenafil 20 mg
order generic prilosec buy omeprazole cheap buy metoprolol medication
micardis 20mg without prescription order telmisartan online molnupiravir where to buy
cialis 20mg generic sildenafil 25mg price viagra medication
cenforce 100mg cheap order naproxen 250mg order chloroquine
purchase provigil generic prednisone 40mg drug buy prednisone 20mg online cheap
buy cefdinir 300 mg online cheap order cefdinir for sale buy lansoprazole for sale
order isotretinoin pills order isotretinoin 20mg online order zithromax without prescription
azithromycin 250mg pills gabapentin 800mg brand order gabapentin 800mg for sale
lipitor 40mg pill proventil ca buy amlodipine 5mg sale
blackjack online money ladbrokes uk furosemide 40mg uk
order protonix for sale buy pantoprazole cheap phenazopyridine order
online casino ventolin inhalator without prescription buy generic albuterol for sale