Laravel custom authentication step by step tutorial

Laravel custom authentication step by step tutorial

Hi friends, in this tutorial we will discuss the step-by-step process of Laravel custom authentication. As we know that Laravel has already provided the default authentication for users along with the prebuilt login and register page. We can create the default authentication with the help of auth command as shown below.

But however, we want to authenticate users manually from different login pages and registration pages. In this case, you can do so by creating the custom controller using the default user model.

Required steps of Laravel custom authentication login logout and registration

Step 1:- Download and install the composer from getcomposer.org
Step2:- Create a laravel project with the help of the composer in the command terminal and start the project in the local development server using the artisan command as shown below one by one.

composer create-project laravel/laravel project-name
cd project-name
php artisan serve

Step 3:- Create default authentication using laravel breeze by running the following command as shown below one by one.

php artisan breeze:install
npm install
npm run dev

Note*:- Laravel breeze is a simple starter kit that provides authentication features including login, registration, password reset, email verification, and password confirmation, etc.

Step 4:- Set up the database in the .env file of your application as shown below

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

Now you can run the below artisan command to migrate the default user table in the database

php artisan migrate

Step 5:- Now create a custom controller using the artisan command in the terminal as shown below

php artisan make:controller CustomAuthController 

Step 6:- Now, set up the authentication routes inside the web.php file under the routes folder as shown below

Route::get('/custom-login',[CustomAuthController::class, 'front_user_login']);
Route::get('/custom-register',[CustomAuthController::class, 'front_user_register']);
Route::post('/custom-login',[CustomAuthController::class, 'userlogin']);
Route::post('/custom-register',[CustomAuthController::class, 'userregister']);
Route::get('/custom-logout',[CustomAuthController::class, 'userlogout']);

Step 7:- Now create two-blade files front-login.blade.php and front-register.blade.php respectively.

front-login.blade.php

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
<hr>
<div class="container" style="width: 50%;">
  <h4 style="text-align: center;">Custom User Login</h4>
  <br><br>
  <form method="POST" action="{{ url('/custom-login') }}">
    @csrf
    <div class="mb-3 row">
    <label for="staticEmail" class="col-sm-2 col-form-label"><b>Email</b></label>
    <div class="col-sm-10">
      <input type="text" name="email" class="form-control" id="staticEmail" value="email@example.com">
    </div>
  </div>
  <div class="mb-3 row">
    <label for="inputPassword" class="col-sm-2 col-form-label"><b>Password</b></label>
    <div class="col-sm-10">
      <input type="password" name="password" class="form-control" id="inputPassword">
    </div>
  </div>
  <br>
  <div class="mb-3 row">
    <label for="staticEmail" class="col-sm-2 col-form-label"></label>
    <div class="col-sm-10">
      <button type="submit" class="btn btn-primary">Submit</button>
    </div>
  </div>
  </form>
  
</div>

<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.2/dist/umd/popper.min.js" integrity="sha384-IQsoLXl5PILFhosVNubq5LC7Qb9DXgDA9i+tQ8Zj3iwWAwPtgFTxbJ8NT4GN1R8p" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.min.js" integrity="sha384-cVKIPhGWiC2Al4u+LWgxfKTRIcfu0JTxR+EQDz/bgldoEyl4H0zUF0QKbrJ0EcQF" crossorigin="anonymous"></script>

front-register.blade.php

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
<hr>
<div class="container" style="width: 50%;">
  <h4 style="text-align: center;">Custom User Register</h4>
  <br><br>
  <form method="POST" action="{{ url('/custom-register') }}">
      @csrf
      <div class="mb-3 row">
        <label for="staticEmail" class="col-sm-2 col-form-label"><b>Name</b></label>
        <div class="col-sm-10">
          <input type="text" name="name" class="form-control" id="staticEmail">
        </div>
      </div>
      <div class="mb-3 row">
        <label for="staticEmail" class="col-sm-2 col-form-label"><b>Email</b></label>
        <div class="col-sm-10">
          <input type="text" name="email" class="form-control" id="staticEmail">
        </div>
      </div>
      <div class="mb-3 row">
        <label for="inputPassword" class="col-sm-2 col-form-label"><b>Password</b></label>
        <div class="col-sm-10">
          <input type="password" name="password" class="form-control" id="inputPassword">
        </div>
      </div>
      <br>
      <div class="mb-3 row">
        <label for="staticEmail" class="col-sm-2 col-form-label"></label>
        <div class="col-sm-10">
          <button type="submit" class="btn btn-primary">Submit</button>
        </div>
      </div>
  </form>
  
</div>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.2/dist/umd/popper.min.js" integrity="sha384-IQsoLXl5PILFhosVNubq5LC7Qb9DXgDA9i+tQ8Zj3iwWAwPtgFTxbJ8NT4GN1R8p" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.min.js" integrity="sha384-cVKIPhGWiC2Al4u+LWgxfKTRIcfu0JTxR+EQDz/bgldoEyl4H0zUF0QKbrJ0EcQF" crossorigin="anonymous"></script>

Step 8:- Now implement the CustomAuthController as shown below

CustomAuthController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\User;
use DB;
use Session;
use Auth;

class CustomAuthController extends Controller
{
    //
    public function front_user_login(){

        return view('auth.front-login');
    }

    public function front_user_register(){

        return view('auth.front-register');
    }
     public function userregister(Request $request)
    {
        if($request->isMethod('post'))
        {
            $data = $request->all();
            //dd($data);
            $usercount = User::where(['email'=>$data['email']])->count();
            //dd($usercount);
            if($usercount>0)
            {
                return redirect('/custom-login')->with('danger','This user already exist!');
            }
            else{
                $newuser = new User;
                $newuser->name = $data['name'];
                $newuser->email = $data['email'];
                $newuser->password = bcrypt($data['password']);
                $newuser->save();

                  if (Auth::attempt(['email' => $data['email'], 'password'=>$data['password']])) {
                      
                       Session::put('frontSession',$data['email']);
                       return redirect('/dashboard')->with('success','Registration Successfull');
                         
                    }  
                
             }

        }
    }

    public function userlogin(Request $request){

                if($request->isMethod('post'))
                {
                    $data = $request->all();
                 if(Auth::attempt(['email'=>$data['email'],'password'=>$data['password']]))
                    {
                        Session::put('frontSession',$data['email']);
                        return redirect('/dashboard')->with('success','Login Successfull');
                    }
                    else{
                        return redirect()->back()->with('danger','Invalid username or Password!');
                    }
                }

    }

    public function userlogout(){
        
         
        Session::forget('frontSession');
        Auth::logout();
        return redirect('/dashboard'); 
        
    }
}

Step 9:- Open the browser and enter the URL’s as shown below

http://127.0.0.1:8000/custom-register
https://127.0.0.1:8000/custom-login

Images of above URL’s

Laravel custom authentication step by step tutorial
Laravel custom authentication step by step tutorial

After successful login, you will be redirected as shown below

Laravel custom authentication step by step tutorial

Read also, CRUD operation in laravel 5.8 step by step for Beginners

Conclusion:- I hope this tutorial will help you understand the concept. If there is any doubt then please leave a comment below.

Leave a Comment

KUPU178

KUPU178

KUPU178

hoki311

hoki311

hoki311

hoki311

hoki311

RATU311

KUPU178

HOKI311

THOR311

THOR311

login hoki311

daftar hoki311

hoki 311

link hoki311

hoki311

RATU311

hoki311

hoki311

hoki311

hoki311

KUPU178

thor311

thor311

thor311

ceri188

ceri188

KUPU178

ceri188

ceri188

link ceri188

KUPU178

KUPU178

KUPU178

kupu178

ceri188

ceri188

ceri188

ceri188

ceri188

THOR311

ceri188

games online ceri188

link login ceri188

ceri188

thor311

RATU311

RATU311

RATU311

sbobet88

sbobet

parlay

mix parlay

judi bola online

thor311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

THOR311

THOR311

THOR311

THOR311

thor311 RTP

thor311

slot gacor

kupu178

slot mahjong

ceri188

sabung ayam

ratu311

THOR311

THOR311

THOR311

thor311 aplikasi

ratu311 sabung ayam

slot gacor

RATU311

ceri188

ceri188

ceri188

ceri188

KUPU178

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

ceri188

ceri188

ceri188 alternatif

ceri188

link ceri188

ceri188

RATU311

THOR311

THOR311

THOR311

RATU311

RATU311

RATU311

Togel Online

KUPU178

Situs Togel

KUPU178

ceri188

ceri188

KUPU178

KUPU178

ceri188

ceri188

ratu311

ceri188

ceri188

ratu311

KUPU178

THOR311

ceri188

ceri188

THOR311

THOR311

RATU311

thor311 tajen bali

thor311 alternatif

situs toto4d

thor311 toto4d

thor311 akses

ceri188

daftar ceri188

cery188

ceri188

slot gacor

kupu 178

kupu178

ceri1888

ceri188

slot online

hoki311

togel online

ceri188

ding dong

ratu311

slot gacor

kupu178

live casino

kupu178

mix parlay

ceri188

judi bola online

thor311 slot

THOR311

RATU311

KUPU178

THOR311

ceri188

ratu311

slot online

hoki311

kupu178

togel online

kupu178

slot gacor

ceri188

judi bola online

ceri188

ratu311

kupu178

RATU311

RATU311

live casino roullete

ceri188

slot pg soft

kupu178

KUPU178

THOR311

RATU311

KUPU178

THOR311

THOR311

thor311 domino qq

thor311 akses

thor dingdong

ceri188

ceri188

kupu178

kupu178

togel

kupu178

mix parlay

ceri188

slot online

kupu178

THOR311

THOR311

KUPU178

kupu178

hoki311

togel online

kupu178

ceri188

ceri188

www.vrcorporate.in

phbalance.vn

togel hongkong

kupu178

slot gacor

kupu178

kupu178

ceri188

kupu178

KUPU178

RATU311

slot online

kupu178

THOR311

KUPU178

slot mahjong ways

hoki311

slot gacor

ceri188

kupu178

Casino Baccarat Online

kupu178

kupu178

ceri188

toto macau

KUPU178

Slot Gacor

KUPU178

RATU311

Mahjong ways

KUPU178

KUPU178

KUPU178

RATU311

RATU311

Toto Togel

KUPU178

CERI188

Slot Sweet Bonanza 2500

KUPU178

RATU311

KUPU178 X SLOT777

judi bola online

THOR311

KUPU178 SLOT TERPERCAYA

KUPU178 TOGEL

TOTO 4D MACAU

KUPU178

THOR311

HOKI311

KUPU178

Slot Mahjong Ways

KUPU178

CERI188

Slot Online Resmi

KUPU178

RATU311 SLOT ONLINE

KUPU178

KUPU178: Link Akses Resmi

KUPU178: Strategi Menang Di Game Live Casino

KUPU178

KUPU178: Minimal 200 Rupiah

THOR311

THOR311

THOR311

THOR311

THOR311

KUPU178

Slot Thailand

CERI188

CERI188

THOR311 RTP

RATU311

CERI188

KUPU178 TOTO SLOT

CERI188 TOTO Singapore

THOR311

CERI188

RATU311: Alternatif karnpuracollege

Thor311 Terbaru

THOR311 MAHJONG

RATU311: BONUS SABUNG AYAM

CERI188: Masuk Akun Slot Super Cepat

THOR311

HOKI311

HOKI311

RATU311

HOKI311

KUPU178

RATU311

RATU311

THOR311

CERI188

CERI188

RATU311

RATU311

RATU311

Game Thor311

HOKI311

HOKI311

RATU311

HOKI311

HOKI311

RATU311 NONTON BOLA

HOKI311 Link QRIS EWALLET

RATU311

HOKI311 Link Apk Resmi

RATU311 judi bola

KUPU178

HOKI311

RATU311

THOR311

HOKI311

KUPU178

KUPU178

RATU311

RATU311

KUPU178

THOR311

KUPU178

RATU311

THOR311