In this tutorial you will learn about the How to Create Custom Auth Login and Registration in Laravel 8 and its application with practical example.
In this How to Create Custom Auth Login and Registration in Laravel 8 Tutorial I will show you how to create custom login and registration system in laravel 8. In this tutorial you will learn to create custom login and registration system in laravel. Though laravel comes with default authentication system we can also create custom login, registration and logout functionality. In the step by step custom authentication tutorial I will show you to create controller, routes, model, and blade views files for custom login, registration and logout functionality.
How to Create Custom Auth Login and Registration in Laravel 8
- Step 1: Create Laravel App
- Step 2: Connect to Database
- Step 3: Set Up Auth Controller
- Step 4: Create Auth Routes
- Step 5: Create Auth Blade View Files
- Step 6: Run Laravel Development Server
Create Laravel App
First of all we need to create a fresh laravel project, download and install Laravel 8 using the below command
1 |
composer create-project --prefer-dist laravel/laravel laravel_demo_app |
Connect to Database
Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.
1 2 3 4 5 6 |
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=database_name DB_USERNAME=database_user_name DB_PASSWORD=database_password |
Now, run following command to migrate database schema.
1 |
php artisan migrate |
Create Custom Auth Controller
Now, lets create a controller named CustomAuthController using command given below –
1 |
php artisan make:controller CustomAuthController |
Now, open app\Http\Controllers\CustomAuthController.php file and carefully place the following code within the file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
<?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
After this, we need to define routes in “routes/web.php” file. Lets open “routes/web.php” file and add the following routes in it.
routes/web.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?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
Now, we need to create a auth directory under resources/views/ folder and create a new login.blade.php file within, and put the following code in resources/views/auth/login.blade.php file: