Search code examples
laravelfirebasegoogle-cloud-firestorelaravel-bladelaravel-9

Submitting a form leads to log out


Information: Language: PHP Framework: Laravel 9 Database: Firebase, firestore

I'm trying to input data from a form in a blade to firestore database. However, it doesn't add in the database. Also after clicking the submit button, it just redirect me to the login page. Basically, it log outs me without even having successfully adding the data input.

What I have tried so far:

  • as per @Aless55, there might be an error. I checked, there's none.

InventoryController:

public function store(Request $request){
        $data = [
            'SKU' => $request->invSKU,
            'Product Name' => $request->invName,
            'Stock' => $request->invStock,
            'Date added' => Carbon\Carbon::now(),
            'Last Update' => Carbon\Carbon::now(),
            'Item sold' => '0',
            'Status' => 'active'
        ];

        $postData = $this->app('firebase.firestore')
                        ->database()
                        ->collection('Inventory')
                        ->newDocument()
                        ->set($data);
        
        if($postData){
            return redirect('admin.inventory');
        } else{
            return redirect('admin.inventory-add');
        }

    }

web.php:

//For Inventory
Route::controller(App\Http\Controllers\InventoryController::class)->group(function(){

    Route::get('inventory/add', 'invAdd')->middleware('user','fireauth');
    Route::post('inventory/add/product', 'store')->name('add.product');
    Route::get('inventory/archive', 'invArchive')->middleware('user','fireauth');

});

form part of inventory-add blade:

<form style="padding-left: 0px;margin-left: -9px;" method="POST" action="{{Route('add.product')}}">
                            @csrf
                            
                            <div style="display: flex;">
                                <div style="padding-right: 0px;margin-right: 29px;">
                                    <h6>Name</h6><input class="form-control" type="text" style="padding-bottom: 0px;" name="invName" autocomplete="off">
                                </div>
                                <div style="padding-right: 0px;margin-right: 29px;">
                                    <h6>SKU</h6><input class="form-control" type="text" style="padding-bottom: 0px;" name="invSKU" autocomplete="off">
                                </div>
                                <div style="padding-right: 0px;margin-right: 29px;">
                                    <h6>Starting Stock</h6><input class="form-control" type="number" name="invStock" autocomplete="off">
                                </div>
           
                            </div>
                            <div style="display: flex;margin-top: 11px;">
                                
                            </div><button class="btn btn-primary" type="submit" style="margin-top: 15px;text-align: right;margin-left: 397px;padding-right: 14px;background: #2f3192;">Submit</button>
                        </form>

middleware:

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Session;

class FirebaseAuth
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle(Request $request, Closure $next)
    {
      $uid = Session::get('uid');
      if ($uid) {
        return $next($request);
      }
      else {
        Session::flush();
        return redirect('/login');
      }

    }
}

Route list:

+--------+-----------+-----------------------------+------------------+-----------------------------------------------------------------------+------------+
| Domain | Method    | URI                         | Name             | Action                                                                | Middleware |
+--------+-----------+-----------------------------+------------------+-----------------------------------------------------------------------+------------+
|        | GET|HEAD  | /                           |                  | Closure                                                               | web        |
|        | GET|HEAD  | api/user                    |                  | Closure                                                               | api        |
|        |           |                             |                  |                                                                       | auth:api   |
|        | GET|HEAD  | carousel                    |                  | App\Http\Controllers\NavBar@carou                                     | web        |
|        |           |                             |                  |                                                                       | user       |
|        |           |                             |                  |                                                                       | fireauth   |
|        |           |                             |                  |                                                                       | auth       |
|        | GET|HEAD  | dashboard                   |                  | App\Http\Controllers\NavBar@dboard                                    | web        |
|        |           |                             |                  |                                                                       | user       |
|        |           |                             |                  |                                                                       | fireauth   |
|        |           |                             |                  |                                                                       | auth       |
|        | GET|HEAD  | email/verify                | verify           | App\Http\Controllers\Auth\ResetController@verify_email                | web        |
|        |           |                             |                  |                                                                       | fireauth   |
|        | GET|HEAD  | home                        | home             | App\Http\Controllers\HomeController@index                             | web        |
|        |           |                             |                  |                                                                       | user       |
|        |           |                             |                  |                                                                       | fireauth   |
|        |           |                             |                  |                                                                       | auth       |
|        | GET|HEAD  | home/profile                | profile.index    | App\Http\Controllers\Auth\ProfileController@index                     | web        |
|        |           |                             |                  |                                                                       | user       |
|        |           |                             |                  |                                                                       | auth       |
|        | POST      | home/profile                | profile.store    | App\Http\Controllers\Auth\ProfileController@store                     | web        |
|        |           |                             |                  |                                                                       | user       |
|        |           |                             |                  |                                                                       | auth       |
|        | GET|HEAD  | home/profile/create         | profile.create   | App\Http\Controllers\Auth\ProfileController@create                    | web        |
|        |           |                             |                  |                                                                       | user       |
|        |           |                             |                  |                                                                       | auth       |
|        | GET|HEAD  | home/profile/{profile}      | profile.show     | App\Http\Controllers\Auth\ProfileController@show                      | web        |
|        |           |                             |                  |                                                                       | user       |
|        |           |                             |                  |                                                                       | auth       |
|        | PUT|PATCH | home/profile/{profile}      | profile.update   | App\Http\Controllers\Auth\ProfileController@update                    | web        |
|        |           |                             |                  |                                                                       | user       |
|        |           |                             |                  |                                                                       | auth       |
|        | DELETE    | home/profile/{profile}      | profile.destroy  | App\Http\Controllers\Auth\ProfileController@destroy                   | web        |
|        |           |                             |                  |                                                                       | user       |
|        |           |                             |                  |                                                                       | auth       |
|        | GET|HEAD  | home/profile/{profile}/edit | profile.edit     | App\Http\Controllers\Auth\ProfileController@edit                      | web        |
|        |           |                             |                  |                                                                       | user       |
|        |           |                             |                  |                                                                       | auth       |
|        | GET|HEAD  | img                         | img.index        | App\Http\Controllers\ImageController@index                            | web        |
|        | POST      | img                         | img.store        | App\Http\Controllers\ImageController@store                            | web        |
|        | GET|HEAD  | img/create                  | img.create       | App\Http\Controllers\ImageController@create                           | web        |
|        | GET|HEAD  | img/{img}                   | img.show         | App\Http\Controllers\ImageController@show                             | web        |
|        | PUT|PATCH | img/{img}                   | img.update       | App\Http\Controllers\ImageController@update                           | web        |
|        | DELETE    | img/{img}                   | img.destroy      | App\Http\Controllers\ImageController@destroy                          | web        |
|        | GET|HEAD  | img/{img}/edit              | img.edit         | App\Http\Controllers\ImageController@edit                             | web        |
|        | GET|HEAD  | inventory                   |                  | App\Http\Controllers\NavBar@inv                                       | web        |
|        |           |                             |                  |                                                                       | user       |
|        |           |                             |                  |                                                                       | fireauth   |
|        |           |                             |                  |                                                                       | auth       |
|        | GET|HEAD  | inventory/add               |                  | App\Http\Controllers\InventoryController@invAdd                       | web        |
|        |           |                             |                  |                                                                       | user       |
|        |           |                             |                  |                                                                       | fireauth   |
|        |           |                             |                  |                                                                       | auth       |
|        | POST      | inventory/add/product       | add.product      | App\Http\Controllers\InventoryController@store                        | web        |
|        |           |                             |                  |                                                                       | auth       |
|        | GET|HEAD  | inventory/archive           |                  | App\Http\Controllers\InventoryController@invArchive                   | web        |
|        |           |                             |                  |                                                                       | user       |
|        |           |                             |                  |                                                                       | fireauth   |
|        |           |                             |                  |                                                                       | auth       |
|        | GET|HEAD  | login                       | login            | App\Http\Controllers\Auth\LoginController@showLoginForm               | web        |
|        |           |                             |                  |                                                                       | guest      |
|        | POST      | login                       |                  | App\Http\Controllers\Auth\LoginController@login                       | web        |
|        |           |                             |                  |                                                                       | guest      |
|        | POST      | login/{provider}/callback   |                  | App\Http\Controllers\Auth\LoginController@handleCallback              | web        |
|        |           |                             |                  |                                                                       | guest      |
|        | POST      | logout                      | logout           | App\Http\Controllers\Auth\LoginController@logout                      | web        |
|        | GET|HEAD  | notification                |                  | App\Http\Controllers\NavBar@notif                                     | web        |
|        |           |                             |                  |                                                                       | user       |
|        |           |                             |                  |                                                                       | fireauth   |
|        |           |                             |                  |                                                                       | auth       |
|        | GET|HEAD  | notification/deleted        |                  | App\Http\Controllers\NotificationController@notifRecDel               | web        |
|        |           |                             |                  |                                                                       | user       |
|        |           |                             |                  |                                                                       | fireauth   |
|        |           |                             |                  |                                                                       | auth       |
|        | POST      | notification/push           | push             | App\Http\Controllers\NotificationController@store                     | web        |
|        |           |                             |                  |                                                                       | user       |
|        |           |                             |                  |                                                                       | fireauth   |
|        |           |                             |                  |                                                                       | auth       |
|        | GET|HEAD  | order                       |                  | App\Http\Controllers\NavBar@order                                     | web        |
|        |           |                             |                  |                                                                       | user       |
|        |           |                             |                  |                                                                       | fireauth   |
|        |           |                             |                  |                                                                       | auth       |
|        | GET|HEAD  | password/confirm            | password.confirm | App\Http\Controllers\Auth\ConfirmPasswordController@showConfirmForm   | web        |
|        |           |                             |                  |                                                                       | auth       |
|        | POST      | password/confirm            |                  | App\Http\Controllers\Auth\ConfirmPasswordController@confirm           | web        |
|        |           |                             |                  |                                                                       | auth       |
|        | POST      | password/email              | password.email   | App\Http\Controllers\Auth\ForgotPasswordController@sendResetLinkEmail | web        |
|        | GET|HEAD  | password/reset              | reset.index      | App\Http\Controllers\Auth\ResetController@index                       | web        |
|        | POST      | password/reset              | reset.store      | App\Http\Controllers\Auth\ResetController@store                       | web        |
|        | GET|HEAD  | password/reset/create       | reset.create     | App\Http\Controllers\Auth\ResetController@create                      | web        |
|        | GET|HEAD  | password/reset/{reset}      | reset.show       | App\Http\Controllers\Auth\ResetController@show                        | web        |
|        | PUT|PATCH | password/reset/{reset}      | reset.update     | App\Http\Controllers\Auth\ResetController@update                      | web        |
|        | DELETE    | password/reset/{reset}      | reset.destroy    | App\Http\Controllers\Auth\ResetController@destroy                     | web        |
|        | GET|HEAD  | password/reset/{reset}/edit | reset.edit       | App\Http\Controllers\Auth\ResetController@edit                        | web        |
|        | GET|HEAD  | password/reset/{token}      | password.reset   | App\Http\Controllers\Auth\ResetPasswordController@showResetForm       | web        |
|        | GET|HEAD  | product                     |                  | App\Http\Controllers\NavBar@prod                                      | web        |
|        |           |                             |                  |                                                                       | user       |
|        |           |                             |                  |                                                                       | fireauth   |
|        |           |                             |                  |                                                                       | auth       |
|        | GET|HEAD  | product/add                 |                  | App\Http\Controllers\ProductController@prodAdd                        | web        |
|        |           |                             |                  |                                                                       | user       |
|        |           |                             |                  |                                                                       | fireauth   |
|        |           |                             |                  |                                                                       | auth       |
|        | GET|HEAD  | product/archive             |                  | App\Http\Controllers\ProductController@prodArchive                    | web        |
|        |           |                             |                  |                                                                       | user       |
|        |           |                             |                  |                                                                       | fireauth   |
|        |           |                             |                  |                                                                       | auth       |
|        | GET|HEAD  | register                    | register         | App\Http\Controllers\Auth\RegisterController@showRegistrationForm     | web        |
|        |           |                             |                  |                                                                       | guest      |
|        | POST      | register                    |                  | App\Http\Controllers\Auth\RegisterController@register                 | web        |
|        |           |                             |                  |                                                                       | guest      |
|        | GET|HEAD  | report                      |                  | App\Http\Controllers\NavBar@report                                    | web        |
|        |           |                             |                  |                                                                       | user       |
|        |           |                             |                  |                                                                       | fireauth   |
|        |           |                             |                  |                                                                       | auth       |
|        | GET|HEAD  | settings                    |                  | App\Http\Controllers\NavBar@settings                                  | web        |
|        |           |                             |                  |                                                                       | user       |
|        |           |                             |                  |                                                                       | fireauth   |
|        |           |                             |                  |                                                                       | auth       |
|        | GET|HEAD  | settings/payment/add        |                  | App\Http\Controllers\SettingsController@payAdd                        | web        |
|        |           |                             |                  |                                                                       | auth       |
|        | GET|HEAD  | shipping                    |                  | App\Http\Controllers\NavBar@ship                                      | web        |
|        |           |                             |                  |                                                                       | user       |
|        |           |                             |                  |                                                                       | fireauth   |
|        |           |                             |                  |                                                                       | auth       |
|        | GET|HEAD  | shipping/add                |                  | App\Http\Controllers\ShippingController@shipAdd                       | web        |
|        |           |                             |                  |                                                                       | user       |
|        |           |                             |                  |                                                                       | fireauth   |
|        |           |                             |                  |                                                                       | auth       |
+--------+-----------+-----------------------------+------------------+-----------------------------------------------------------------------+------------+

Solution

  • I have this side menu and the logout part used to be like this

    <li class="nav-item"><a class="nav-link" href="" onclick="event.preventDefault();
        document.getElementById('logout-form').submit();"><span></span><strong style="font-size: 16.6px;"><img src="{{ asset('build/img/vectors/logout%20icon.png?h=dbf300e18e83c66a9b08989511fc388a')}}">&nbsp; &nbsp;Sign out</strong></a></li>
    <form id="logout-form" action="{{ route('logout') }}" method="POST" class="d-none">
        @csrf
    </form>
    

    I removed the form and leave the {{ route('logout') }}

    and my solution:

    <li class="nav-item"><a class="nav-link" href="{{ route('logout')}}"><span></span><strong style="font-size: 16.6px;"><img src="{{ asset('build/img/vectors/logout%20icon.png?h=dbf300e18e83c66a9b08989511fc388a')}}">&nbsp; &nbsp;Sign out</strong></a></li>