• 18 Jan, 2025

How to Make Login in Laravel Authentication with Laravel

How to Make Login in Laravel Authentication with Laravel

How to Make Login in Laravel Authentication with Laravel

In our previous tutorial, we have handled Laravel Register Data using mysql,jQuery. In this tutorial, we will handle user  login With html form in Laravel,  MySQL and  jQuery. The registration form submit and store into MySQL database  

We have handled tutorial in very easy steps. So let’s start the coding

Here put some sample code. for full code end of post click download now

 Step 1: Add the JavaScript, jQuery, and Bootstrap files.   
   

 <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
 <link rel="stylesheet" href="{{ asset('css/app.css') }}">
 <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.6.0/css/all.min.css">


Step 2.  Create the HTML for the login Form .  
    

 <div class="col-12">
               <form action="{{ route('login.post') }}" method="post">
                   @csrf
                   @if (Session::has('error'))
                       <p class="text-danger">{{ Session::get('error') }}</p>
                   @endif
                   <div class="form-group">
                       <label for="email">Email</label>
                       <input type="email" name="email" class="form-control"
                        @if (isset($_COOKIE["email"]))
                        value="{{($_COOKIE["email"])}}"
                       
                        @else
                        value="{{ old('email') }}"
                        @endif
                       
                        placeholder="Email"
                            />
                       @if ($errors->has('email'))
                           <p class="text-danger">{{ $errors->first('email') }}</p>
                       @endif
                   </div>
                   <br>
                   <div class="form-group">
                       <label for="password">Password</label>
                       <input type="password" name="password" class="form-control" placeholder="Password" 
                       @if (isset($_COOKIE["password"]))
                       value="{{($_COOKIE["password"])}}"
                       @endif
                       />
                       @if ($errors->has('password'))
                           <p class="text-danger">{{ $errors->first('password') }}</p>
                       @endif
                   </div>
                   <br>
                   <div class="row">
                       <div class="col-6">
                           <div class="form-check">
                               <input type="checkbox" class="form-check-input border-dark" name="remember"
                                   id="remember"/>
                               <label class="form-check-label" for="remember">Remember Me</label>
                               @if ($errors->has('remember'))
                                   <p class="text-danger">{{ $errors->first('remember') }}</p>
                               @endif
                           </div>
                       </div>
                       <div class="col-6 text-center">
                           <input class="btn btn-primary" type="submit" value="Login">
                       </div>
                   </div>
                   <br>
                   <br>
               </form>
               <p>Don't have an account? <a href="{{ route('register') }}">Register</a></p>
           </div>
   

Sample screen short

 

How to Make Registration in Laravel Authentication with Laravel-1
    
   Step 3: Use jQuery Ajax to validate the form and submit it.   
  

  $request->validate([
           'email' => 'required|email',
           'password' => 'required',
       ]);

   
   Step 5: submit data & check login details exist on server or not.  
    

  $user = User::where('email', $request->email)->first();
       if ($user) {
           if (Auth::attempt(['email' => $request->email, 'password' => $request->password], $request->remember)) {
               Alert::success('success', 'User Login SuccessFully');
               if (isset($request['remember']) && !empty($request['remember'])) {
                   setcookie("email", $request['email'], time() + 3600);
                   setcookie("password", $request['password'], time() + 3600);
               } else {
                   setcookie("email", "");
                   setcookie("password", "");
               }
               return redirect()->route('main_page');
           } else {
               Alert::error('error', 'Your Password is Incorrect');
               return redirect()->route('login');
           }
       } else {
           Alert::error('error', 'Your Email is Incorrect');
           return redirect()->route('login');
       }

Sample screen short

How to Make Registration in Laravel Authentication with Laravel1


4. Run Laravel Development Server

Now we have successfully completed all the steps Now run the Laravel development server for this command.

 php artisan serve

And then open your web browser and browser or visit the following URL to test

 http://127.0.0.1:8000/login   


Download Full Code HERE     
     

Download Fome Here