• Techblog369, India
  • March 28, 2023

How To Configure A Sendinblue SMTP Relay in Postfix?

Fix Email Issues in CyberPanel with SMTP Relay Setup Free Method. First Create a email using your Cyberpanel Under Emai Tab Create Email Second go to the SSL tab and click …

Create a simple password strength indicator with JavaScript

You’ve probably seen many examples of password strength indicators around the web. They let users know the password they’re using is weak and indicate how the strength changes when it’s modified. …

In this article, I am going to create a Blog project using Laravel 8 step by step

Complete Laravel blog project step by step Part2

Now in this article, I am going to create AdminController and setup, Admin Panel

php artisan make:controller AdminController

Now we will create a login function for Admin login in AdmnController

    public function login()

    {

    return view('backend.login');

    }

Now we have to create a login file for the admin login, So, go to the resource folder and under the view, folder create a folder named backend and under the backend folder create login.blade.php file

Now we have to create a route for Admin login, So, go to the routes→web.php file and create a route

    First import AdminController

    use App\Http\Controllers\AdminController;

    then make route like below

    Route::get("/admin/login",[AdminController::class,'login']);

Note: /admin/login is the URL and login is the method which is defined in AdminController

Run php artisan serve and check the url http://127.0.0.1:8000/admin/login

Now we are using bootstrap Admin Panel so we need to download it we have already downloaded the admin templatehttps://themes.getbootstrap.com/product-category/admin-dashboard/

First, we have to upload js and CSS files or folders for the Admin Login. we have copied the folder and pasted it to the backend folder under the public folder

public→backend→your files or folder JS and CSS

Now we have to link or load the JS & CSS with the special helper like below. We can access static files like below. Static files are should be in the public folder.

{{asset(‘backend’)}}/

<link href=”{{asset(‘backend’)}}/css/styles.css” rel=”stylesheet” />

Now we will create model for Admin with migration on below command

php artisan make:model Admin -m

Once successfully create model and database migration table paste the below code in up function

public function up()
   {
       Schema::create('admins', function (Blueprint $table) {
           $table->id();
           $table->string('username');
           $table->string('password');
           $table->timestamps();
       });
   }
php artisan migrate

Let’s create the seeder for the Admin model

We can manually insert the data into the admin table but we can’t insert passwords with the encrypted format. with the help of seeder, we can insert the demo data

    php artisan make:seeder AdminsTableSeeder

    Seeder created successfully.

Now insert the admin data, So, go to the seeder folder under the database→seeders and open newly created  AdminsTableSeeder.php file

First, we need to import DB and Hash Facades like below

    use Illuminate\Support\Facades\DB;     /* DB Access  */
    use Illuminate\Support\Facades\Hash; /* Password hashing */

    public function run()

    {

    DB::table('admins')->insert([

    'username'=>'admin',

    'password'=>Hash::make('123456789')


     

    ]);

    }

Once complete run the below command

php artisan db:seed --class=AdminsTableSeeder

Now we will create a login functionality So, go to the AdminController and paste the below code and first, you have to import Admin Model

    use App\Models\Admin;

       public function submit_login(Request $request)
       {
           $request->validate([
               'username'=>'required',
               'password'=>'required'

           ]);
           $userCheck=Admin::where(['username'=>$request->username,'password'=>$request->password])->count();
           if($userCheck>0){
               $adminData=Admin::where(['username'=>$request->username,'password'=>$request->password])->first();
               session(['adminData'=>$adminData]);
                return redirect('admin/dashboard');
           }else{
                return redirect('admin/login')->with('error',"Invalid Username and Password");
           }

    function dashboard(){

    return view('backend.dashboard');

    }
         

       }

Details of code

submit_login is a function name

Request as a parameter for the getting from form values

Validate will check both username and password field is required

Now we run the query and check the username and password is matched or not which came from submitting the login form.

and used aggregate function count. if the data is greater than 0 again we run the same Query with first() because the username may be the same for another user. it will check the username then his password and save the data in session as adminData.

Now we create a dashboard function and simply return to the dashboard.

Now we have to create a route both for submitting and dashboard as like below

Route::get("/admin/dashboard",[AdminController::class,'dashboard']);
Route::post("/admin/login",[AdminController::class,'submit_login']);

After creating the route, go to the login.blade.php file and set @csrf, method

login.blade.php

@if($errors)
                                   @foreach($errors->all() as $error)
                                   <p class="text-danger">{{$error}}</p>
                                 @endforeach
                                   @endif
                                   
                                   @if(Session::has('error'))
                                   <p class="text-danger">{{session('error')}}</p>
       @endif

                                 <form method="POST" action="">
                                           @csrf
                                           <div class="form-floating mb-3">
                                               <input class="form-control" id="inputEmail" name="username" type="text" placeholder="UserName" />
                                               <label for="inputEmail">Email address</label>
                                           </div>
                                           <div class="form-floating mb-3">
                                               <input class="form-control" id="inputPassword" name="password" type="password" placeholder="Password" />
                                               <label for="inputPassword">Password</label>
                                           </div>
                                           <div class="form-check mb-3">
                                               <input class="form-check-input" id="inputRememberPassword" type="checkbox" value="" />
                                               <label class="form-check-label" for="inputRememberPassword">Remember Password</label>
                                           </div>
                                           <div class="d-flex align-items-center justify-content-between mt-4 mb-0">
                                               <a class="small" href="password.html">Forgot Password?</a>
                                               <!-- <a class="btn btn-primary" href="index.html">Login</a> -->
                                               <input type="submit" class="btn btn-primary btn-block"/>
                                           </div>
                                       </form>

Note: We are using the POST method for submitting the data.POST method is secure rather than GET because GET method passes the data in URL. 

@csrf generate a hidden token without csrf we cant submit the form data.@csrf is Laravel default functionality we can also disable csrf for a specific route. 

input name set by username for username and password for password and finally set input type to submit.

Now if the username and password is matching from your database you can login and redirect to the dashboard page.

Part 2 finished. please wait for part3

Author

nw.ippm@gmail.com

Leave a Reply

Your email address will not be published. Required fields are marked *