Laravel 7.x Custom Login And Registration Example

Hello Artisan,

In this Laravel 7 custom login and registration example, I will show you how to create a complete custom authentication system in laravel. You know that Laravel provides custom authentication system, but in this example, we are going to create our own custom login and registration system with logout.

So if you do not know how to create custom authentication in laravel 7 application, then this laravel 7 custom authentication tutorial is for you. In this tutorial, I am not going to verify email before registration and login. Just I will create simple login and registration mechanism. 

If you want to use the Laravel default email verification system, you can check the 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 a fresh laravel project to complete our tutorial. So download it using the below command.

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

 

Step 2: Setup route

After downloading we have to set up 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 a 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 a blade file for the login and registration form. So create it.

resourses\views\login.blade.php

 

Now paste this below code to your registration form.

resourses/views/register.blade.php

 

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

 

Now all are ok. If you want to redirect the user to the custom path after login then you have to do just one thing. Visit the 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-auth #laarvel-custom-login