Laravel 8 Custom Login And Registration Example

Hello Artisan
In this laravel custom auth and regsiter tutorial i am going to show you laravel 8 custom authentication. Though laravel provides auth scaffolding but in this tutorial we will create our own created custom login and registration features in our application. 

Laravel provides the basic auth there includes the basic login, registration, and password reset functionalities. These functionalities aslo can be easily customized.

But i this laravel 8 login and registration example tutorial i will show you how to make login and registration without using laravel basic auth. That mean it will be our custom login and registration.

In this tutorial we won't verify email before registration and login. Just we will create simple login and registration mechanism. 

So let's create our own custom laravel login and registration. I will show you from scratch. So don't worry. Just follow the below step to complete this custom login and registration.

If you want to use Laravel deafult email verification system, you can check below link.

 

Read also :  Email Verification after Registration in Laravel

 

Let's start our Laravel custom auth and registration tutorial.

Step 1: Install Laravel 

We need fresh laravel project to complete our tutorial. So download it using below command.

composer create-project --prefer-dist laravel/laravel blog

 

Step 2: Setup route

After downloading we have to setup our route.

routes\web.php


Route::namespace('Auth')->group(function () {
  Route::get('/login','LoginController@show_login_form')->name('login');
  Route::post('/login','LoginController@process_login')->name('login');
  Route::get('/register','LoginController@show_signup_form')->name('register');
  Route::post('/register','LoginController@process_signup');
  Route::post('/logout','LoginController@logout')->name('logout');
});

 

Step 3:  Create LoginController

Now we need login controller to write our custom login and registration code. So create it in this following path. 

App\Http\Controllers\Auth\LoginController.php

namespace App\Http\Controllers\Auth;

use App\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class LoginController extends Controller
{  
    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }
    
    public function show_login_form()
    {
        return view('login');
    }
    public function process_login(Request $request)
    {
        $request->validate([
            'name' => 'required',
            'password' => 'required'
        ]);

        $credentials = $request->except(['_token']);

        $user = User::where('name',$request->name)->first();

        if (auth()->attempt($credentials)) {

            return redirect()->route('home');

        }else{
            session()->flash('message', 'Invalid credentials');
            return redirect()->back();
        }
    }
    public function show_signup_form()
    {
        return view('backend.register');
    }
    public function process_signup(Request $request)
    {   
        $request->validate([
            'name' => 'required',
            'email' => 'required',
            'password' => 'required'
        ]);
 
        $user = User::create([
            'name' => trim($request->input('name')),
            'email' => strtolower($request->input('email')),
            'password' => bcrypt($request->input('password')),
        ]);

        session()->flash('message', 'Your account is created');
       
        return redirect()->route('login');
    }
    public function logout()
    {
        \Auth::logout();

        return redirect()->route('login');
    }
}

 

Step 4: Create Blade for View

Now in this step we need to create blade file for login and registrtaion form. So create it.

resourses\views\login.blade.php

 

Now paste this below code to your register form.

resourses/views/register.blade.php

 

Now everything is done for login and registration. Just we need to setup logout system for completing it. So create it. Paste it where you want to show logout button.

 

Now all are ok. If you want to redirect user to custom path after login then you have to do just one thing. Visit this following path and change the path. 

app/Providers/RouteServiceProvider.php

public const HOME = '/give_your_required_path_here';

 

Read also : Laravel 7 Activate Account after Email Verification Example

 

Hope it can help you.

 

#laravel #laravel-8x #laravel-custom-login