Laravel 9 Custom Login and Registration Tutorial: Step-by-Step Guide with Examples

Published on : May 11,2023
Laravel 9 Custom Login and Registration Tutorial: Step-by-Step Guide with Examples

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.

Step 1 Create Laravel App:

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.

 

Step 2 Connect to the Database:

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.

 

Step 3 Set Up Auth Controller:

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');
	}
}

 

Step 4 Create Auth Routes:

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');

 

Step 5 Create Auth Blade View:

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.

 

Step 6: Run Laravel Development Server

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

Abhay Dudhatra
Abhay Dudhatra
I am a full-stack developer who is passionate about creating innovative solutions that solve real-world problems. With expertise in technologies such as PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter, and Bootstrap, I love to share my knowledge and help others in the industry through writing tutorials and providing tips. Consistency and hard work are my mantras, and I constantly strive to improve my skills and stay up-to-date with the latest advancements in the field. As the owner of Open Code Solution, I am committed to providing high-quality services to my clients and helping them achieve their business objectives.


26 Comments

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


Leave a comment

We'll never share your email with anyone else. Required fields are marked *

Related Articles