Laravel 8 – using AuthCheck middleware and just trying to check Has Username in session but it gives an error Error Description is below

  laravel, laravel-8, middleware, php

using AuthCheck middleware and just trying to check Has Username in session but it gives an error Error Description is below

Error
ErrorException
Attempt to read property "headers" on string LoginController
C:xampphtdocslarave8authvendorlaravelframeworksrcIlluminateFoundationHttpMiddlewareVerifyCsrfToken.php:191

<?php

namespace AppHttpControllers;

use IlluminateHttpRequest;
use IlluminateSupportFacadesHash;
use AppModelsUser;
use IlluminateSupportFacadesSession;

class LoginController extends Controller
{
    function login()
    {
        return view('login.login');
    }
    function checkLogin(request $request)
    {
        $validate=$request->validate([
            'email'=>'required|email',
            'password'=>'required|min:5|max:15'
        ]);
        
        $user=User::where('email','=',$request->email)->first();
        if(!$user)
        {
            return back()->with('fail','we  don't recognised your email');
        }
        else
        {
            
            if(Hash::check($request->password,$user->password))
            {
                Session::put('username',$request->email);
                return redirect('/dashboard');
            }
            else
            {
                return back()->with('fail','Password don't match');
            }
        }
    
    }

    function dashboard()
    {
        return view('login.dashboard');
    }
}
?>

AuthCheck.php

<?php

namespace AppHttpMiddleware;

use Closure;
use IlluminateHttpRequest;

class AuthCheck
{
    /**
     * Handle an incoming request.
     *
     * @param  IlluminateHttpRequest  $request
     * @param  Closure  $next
     * @return mixed
     */
    public function handle(Request $request, Closure $next)
    {
         if($request->session()->has('username'))
         {
            return "true";
         }
         else
         {
            return "false";
         }
         
    }
}
?>

web.php

<?php

use IlluminateSupportFacadesRoute;
use AppHttpControllersLoginController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/


Route::get('/login',[LoginController::class,'login']);
Route::post('/checkLogin',[LoginController::class,'checkLogin'])->name('checkLogin');
Route::group(['middleware'=>['AuthCheck']],function(){
    Route::get('/user', [UserController::class, 'index']);
    Route::get('/dashboard',[LoginController::class,'dashboard']);
});
?>

Source: Laravel

Leave a Reply