Add middleware auth for livewire form submit method

In my Laravel project I render the livewire component for sumit form. But I want to apply the middleware condition for form. Mean if user is not logged in then when he hit the submit button then login page render it can anyone tells me how to solve this problem…
This is my livewire component for form submit

<?php

namespace App\Http\Livewire\Rfq;

use App\Rfq;
use App\City;
use App\currency;
use App\Tbl_units;
use Livewire\Component;
use App\Tbl_productcategory;
use App\Tbl_product_subcategory;

class RfqForm extends Component
{
    
    public $product_name;
    public $product_quantity;
    public $unit_id;
    public $currency_id;
    public $purchase_price;
    public $city;
    public $details;
    public $isChecked;

    public $product_category_id;
    public $subcategories = [];
    public $sub_category_id;


    public function store()
    {
        $validatedData = $this->validate([
            'product_name' => 'required',
            'product_category_id' => 'required',
            'sub_category_id' => 'required',
            'product_quantity' => 'required',
            'unit_id' => 'required|not_in:0',
            'purchase_price' => 'required',
            'city' => 'required|',
            'isChecked' => 'required|accepted',
        ]);

        $validatedData['details'] = $this->details;

        auth()->user()->rfqs()->create($validatedData);
        
        session()->flash('message', 'RFQ submitted successfully.');
        
        $this->reset();
    }

    public function render()
    {
        if(!empty($this->product_category_id)) {
            $this->subcategories = Tbl_product_subcategory::where('procat_id', $this->product_category_id)->get();
        }

        return view('livewire.rfq.rfq-form')->with([
            'categories' => Tbl_productcategory::has('tbl_product_subcategory')->get(),
        ]);
    }
}

auth()->guest(), auth()->check()…i think you can use them

In general, it’s laravel question not livewire and as @prospero said you can use auth()->check() to check the logged-in users and in the routes layer you can check by Route::get('…', ...)->middleware('auth')

Or, if you want something precise you can make a normal controller, return a view and inside the view call the livewire component like so:

class ControllerName extends Controller
{
    public funciton __construct()
   {
      $this->middleware('auth');
   }

   public funciton create()
  {
      return view('your-blade-file');
   }
}

In your-blade-file:

<livewire:your-component/>

And continue what you are trying to do with your component.

   public function store()
   {
      if (!auth()->check()) {
          return abort(403); // or you can return the user to the login page
      }
   }
1 Like

do you have any idea or example code…how make a form public with authentication means suppose I a Form where user fill the form and cllick the submit button.
But Condition is if user is not logged in then while clicking on submit button they redirect to the login page and after login the previous filled form submitted and redirect back to the same form with success message.

I don’t know why you do such a thing! This is not stands as a user experience.

Anyhow, you can do a redirect hidden input in your login page and when the user redirected to the login page the redirected input value filled with the referee link, after the user logged-in successfully make the redirection to the referrer link.
For example:

@auth
   <button type="submit">Submit</button>
@else
  <a href="{{route('login', ['redirect' => request()->fullUrl()]}}">Submit</a>
@endauth

And in your login blade

<input type="hidden" name="redirect"/>

In your login controller

public function login()
{
   ....
   if (request->has('redirect')) {
      return redirect()->to(request()->input('redirect'));
   }
   ...
}

I hope you got the idea.

PS: If your app requires users logged-in in some pages/forms, you should let them know before they start typing

1 Like

PS : If your app requires users logged-in in some pages/forms, you should let them know before they start typing

Means simple auth middleware apply…right?

Sor for these of condition we have make some changes on LoginController…?
BTW thank you for your response I got idea…I will try this technique…

You are correct, you should make some changes. I notice in some of my applications when action requires to be authenticated, after the login process laravel redirect you automatically to the previous page, but I don’t know what’s technique behind this behavior. In many cases this is the idea.

1 Like

Yes I’m trying to apply same behaviour in Livewire component…But I think I have to make some changes on LoginController…