Wednesday, 3 August 2022

Custom Auth Login Registration Logout in Laravel 9

Laravel 9 Custom Login Registration Video Tutorial Part 1



Laravel 9 Custom Login Registration Video Tutorial Part 2



Laravel 9 Custom Login Registration Video Tutorial Part 3



This is Laravel 9 Custom Login Logout and Registration Tutorial, so by this tutorial, we will show you how to create custom Authentication Login Logout and Registration in Laravel 9 Application. So here we will use Laravel 9 Custom Login and Registration.

If you have use Laravel framework for your web development then Laravel 9 framework has provides in built Authentication by using jetstream and UI package. But in some condition we have to create our own Login, Regstration, dashboard and logout. So This tutorial will help you to create custom Login Registration in Laravel 9 framework and here in this tutorial we will create step by step custom login and registration page in Laravel 9 application.

  1. Install Laravel 9 Application
  2. Make Database Connection
  3. Create Controller
  4. Create View Blades Files
  5. Set Controller Method Route
  6. Run Laravel 9 Application

Custom Auth Login Registration Logout in Laravel 9




Step 1 : Install Laravel 9 Application


First for Crate Custom Login and Registration in Laravel 9 framework, then first we have to download fresh Laravel 9 application. So for this, we have goes to command prompt and run following command. This command will create custom_login directory and under that directory, it will download Laravel 9 application.


composer create-project laravel/laravel custom_demo


Step 2 : Make Database Connection


After download Laravel 9 Application for Create Custom Login and Registration, now we have to make MySQL database connection, So we have to open .env file and under this file, we have to define following MySQL database configuration which you can seen below and it will make MySQL database connection in Laravel 9 Application.


DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=testing
DB_USERNAME=root
DB_PASSWORD=


After Make MySQL database connection, now we need to create users table in connected MySQL database. So we goes to command prompt and run following command. This command will create users tables in MySQL database.


php artisan migrate


Step 3 : Create Controller


Now in this steps for handle http request of Laravel 9 Custom Login and Registration Application, we have to create controller. So for this, we have goes to command prompt and run following command, which will create SampleController.php file under app/Http/Controllers directory and under this file, we have to add following code.

app/Http/Controllers/SampleController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Hash;
use Session;
use App\Models\User;
use Illuminate\Support\Facades\Auth;

class SampleController extends Controller
{
    function index()
    {
        return view('login');
    }

    function registration()
    {
        return view('registration');
    }

    function validate_registration(Request $request)
    {
        $request->validate([
            'name'         =>   'required',
            'email'        =>   'required|email|unique:users',
            'password'     =>   'required|min:6'
        ]);

        $data = $request->all();

        User::create([
            'name'  =>  $data['name'],
            'email' =>  $data['email'],
            'password' => Hash::make($data['password'])
        ]);

        return redirect('login')->with('success', 'Registration Completed, now you can login');
    }

    function validate_login(Request $request)
    {
        $request->validate([
            'email' =>  'required',
            'password'  =>  'required'
        ]);

        $credentials = $request->only('email', 'password');

        if(Auth::attempt($credentials))
        {
            return redirect('dashboard');
        }

        return redirect('login')->with('success', 'Login details are not valid');
    }

    function dashboard()
    {
        if(Auth::check())
        {
            return view('dashboard');
        }

        return redirect('login')->with('success', 'you are not allowed to access');
    }

    function logout()
    {
        Session::flush();

        Auth::logout();

        return Redirect('login');
    }
}






Step 4 : Create View Blades Files


In this steps, we need to create blade views file for display custom Login Registration page output in the browser. So here we have goes into resources/views directory and under this we have to create blades files for display login, registration and dashboard page files and we will also create master template files also. Below we will create files one by one.

resources/views/main.blade.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Laravel 9 Custom Login Registration</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>

    <nav class="navbar navbar-light navbar-expand-lg mb-5" style="background-color: #e3f2fd;">
        <div class="container">
            <a class="navbar-brand mr-auto" href="#">Laravel 9 Custom Login Registration</a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarNav">
                    
                <ul class="navbar-nav">
                    @guest

                    <li class="nav-item">
                        <a class="nav-link" href="{{ route('login') }}">Login</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="{{ route('registration') }}">Register</a>
                    </li>

                    @else

                    <li class="nav-item">
                        <a class="nav-link" href="{{ route('logout') }}">Logout</a>
                    </li>

                    @endguest
                </ul>
                
            </div>
        </div>
    </nav>
    <div class="container mt-5">

        @yield('content')
        
    </div>
    
</body>
</html>


resources/views/login.blade.php

@extends('main')

@section('content')

@if($message = Session::get('success'))

<div class="alert alert-info">
{{ $message }}
</div>

@endif

<div class="row justify-content-center">
	<div class="col-md-4">
		<div class="card">
			<div class="card-header">Login</div>
			<div class="card-body">
				<form action="{{ route('sample.validate_login') }}" method="post">
					@csrf
					<div class="form-group mb-3">
						<input type="text" name="email" class="form-control" placeholder="Email" />
						@if($errors->has('email'))
							<span class="text-danger">{{ $errors->first('email') }}</span>
						@endif
					</div>
					<div class="form-group mb-3">
						<input type="password" name="password" class="form-control" placeholder="Password" />
						@if($errors->has('password'))
							<span class="text-danger">{{ $errors->first('password') }}</span>
						@endif
					</div>
					<div class="d-grid mx-auto">
						<button type="subit" class="btn btn-dark btn-block">Login</button>
					</div>
				</form>
			</div>
		</div>
	</div>
</div>

@endsection('content')


resources/views/registration.blade.php

@extends('main')

@section('content')

<div class="row justify-content-center">
	<div class="col-md-4">
		<div class="card">
		<div class="card-header">Registration</div>
		<div class="card-body">
			<form action="{{ route('sample.validate_registration') }}" method="POST">
				@csrf
				<div class="form-group mb-3">
					<input type="text" name="name" class="form-control" placeholder="Name" />
					@if($errors->has('name'))
						<span class="text-danger">{{ $errors->first('name') }}</span>
					@endif
				</div>
				<div class="form-group mb-3">
					<input type="text" name="email" class="form-control" placeholder="Email Address" />
					@if($errors->has('email'))
						<span class="text-danger">{{ $errors->first('email') }}</span>
					@endif
				</div>
				<div class="form-group mb-3">
					<input type="password" name="password" class="form-control" placeholder="Password" />
					@if($errors->has('password'))
						<span class="text-danger">{{ $errors->first('password') }}</span>
					@endif
				</div>
				<div class="d-grid mx-auto">
					<button type="submit" class="btn btn-dark btn-block">Register</button>
				</div>
			</form>
		</div>
	</div>
</div>

@endsection('content')


resources/views/dashboard.blade.php

@extends('main')

@section('content')

<div class="card">
	<div class="card-header">Dashboard</div>
	<div class="card-body">
		
		You are Login in Laravel 9 Custom Login Registration Application.
	</div>
</div>

@endsection('content')


Step 5 : Set Controller Method Route


Under this step, we need to create custom route for SampleController.php files method. So we need to open routes/web.php file and under this file we have to all following route.

routes/web.php

<?php

use Illuminate\Support\Facades\Route;

use App\Http\Controllers\SampleController;

/*
|--------------------------------------------------------------------------
| 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('/', function () {
    return view('welcome');
});

Route::controller(SampleController::class)->group(function(){

    Route::get('login', 'index')->name('login');

    Route::get('registration', 'registration')->name('registration');

    Route::get('logout', 'logout')->name('logout');

    Route::post('validate_registration', 'validate_registration')->name('sample.validate_registration');

    Route::post('validate_login', 'validate_login')->name('sample.validate_login');

    Route::get('dashboard', 'dashboard')->name('dashboard');

});



Step 6 : Run Laravel 9 Application


This is last step of Laravel 9 Custom Login, Registration and Logout Application and after follow all above steps, now for check output in the browser, we have to start Laravel server. So for this we have goes to command prompt and run following command.


php artisan serve


So after run this command it will start Laravel server and provide us base url of our Laravel 9 Custom Login Registration Application. So we have to copu that url and open in browser for check output in the browser. So for check demo of Laravel 9 Custom Login Registration application, we have to open following url in the browser.


http://127.0.0.1:8000/login


And lastly, if you have follow this tutorial step by step, then you can able to build custom Login Registration page in Laravel 9 Framework from scratch. We hope this tutorial will help you and you can something learn new things from this tutorial. So best of Luck for this Laravel 9 Custom Login Registration tutorial.





0 comments:

Post a Comment