Laravel Custom Auth Login and Registration Example

Hello artisan,

I will show you an example of laravel custom login and registration example step by step. This tutorial is a simple example of laravel custom login authentication. I will help you to give an example of laravel custom login and registration. Here we will teach you laravel custom registration and login.

Laravel provides auth using jetstream and UI package. but sometimes we need to create our own login, registration, dashboard, and log out then you will know how to create step by step custom login and registration page in laravel application. you can easily create custom login and registration with laravel 6, laravel 7, laravel 8 and laravel 9 version.

 

Step 1: Install Laravel

In this first step, l need to get a fresh Laravel version application using the bellow command, So open your terminal OR command prompt and run the bellow command:

composer create-project laravel/laravel example-app

 

Step 2: Connect Database

In this step, we need to add database configuration in the .env file. so let's add the following details and then run the migration command:

.env

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel9_blog
DB_USERNAME=root
DB_PASSWORD=password

 

Next, run the migration php artisan migrate command to create a users table. 

 

Read also: Laravel 9 Livewire Datatable with Rappasoft Package

 

Step 3: Create Route

In this step, we need to create a custom route for login, register, home, and logout. so open your routes/web.php file and add the following route.

routes/web.php

use Illuminate\Support\Facades\Route;
  
use App\Http\Controllers\Auth\AuthController;
  
/*
|--------------------------------------------------------------------------
| 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('login', [AuthController::class, 'index'])->name('login');
Route::post('post-login', [AuthController::class, 'postLogin'])->name('login.post'); 
Route::get('registration', [AuthController::class, 'registration'])->name('register');
Route::post('post-registration', [AuthController::class, 'postRegistration'])->name('register.post'); 
Route::get('dashboard', [AuthController::class, 'dashboard']); 
Route::get('logout', [AuthController::class, 'logout'])->name('logout');

 

Step 4: Create Controller

in this step, we need to create AuthController and add the following code to that file:

app/Http/Controllers/Auth/AuthController.php

namespace App\Http\Controllers\Auth;
  
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Session;
use App\Models\User;
use Hash;
  
class AuthController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index()
    {
        return view('auth.login');
    }  
      
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function registration()
    {
        return view('auth.registration');
    }
      
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function postLogin(Request $request)
    {
        $request->validate([
            'email' => 'required',
            'password' => 'required',
        ]);
   
        $credentials = $request->only('email', 'password');
        if (Auth::attempt($credentials)) {
            return redirect()->intended('dashboard')
                        ->withSuccess('You have Successfully loggedin');
        }
  
        return redirect("login")->withSuccess('Oppes! You have entered invalid credentials');
    }
      
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function postRegistration(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('Great! You have Successfully loggedin');
    }
    
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function dashboard()
    {
        if(Auth::check()){
            return view('dashboard');
        }
  
        return redirect("login")->withSuccess('Opps! You do not have access');
    }
    
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function create(array $data)
    {
      return User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'password' => Hash::make($data['password'])
      ]);
    }
    
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function logout() {
        Session::flush();
        Auth::logout();
  
        return Redirect('login');
    }
}

 

Step 5: Create Blade Files

Now, we need to create blade files for layout, login, register, and home page. so let's create one by one file:

resources/views/auth/login.blade.php

 

resources/views/auth/registration.blade.php

 

resources/views/dashboard.blade.php

 

resources/views/layout.blade.php

 

Read also: Laravel 9 Rest API Development with Laravel Orion

 

Now all are set to go. you can check now.

 

author-image
Facebook Github
A web enthusiastic, a self-motivated full-stack software engineer from Dhaka, Bangladesh with experience in developing applications using Laravel , React and Vue js