#are Middlewares mendatory for laravel web ?

7 messages · Page 1 of 1 (latest)

azure girder
#

Hello im using web.php for the first time and initially i was making api endpoints and had installed and used jwt but then the client said he wants to do everything with laravle view and backend. So now i modified my AuthConrtoller ``` public function login(LoginRequest $request)
{
// Validation des données de la requête
$request->validate([
'email' => 'required|email',
'password' => 'required',
]);

    $user = User::where('email', $request->email)->first();

    // Chercher l'utilisateur par email
    if ($user && app(Sha512Hasher::class)->check($request->password, $user->password)) {
        Auth::login($user);
        $request->session()->regenerate();

        return redirect()->route('home')->with('success', 'Vous etes connecte');
        // Authentification réussie
        // $token = JWTAuth::fromUser($user);
        // return response()->json([
        //     'ok' => true,
        //     'data' => UserResource::make($user),
        //     'message'=> 'User logged in successfully',
        //     'authorisation' => [
        //         'token' => $token,
        //         'type' => 'bearer',
        //     ]
        // ]);
    }

    return back()->withErrors([
        'email' => 'Les informations d\'identification fournies ne correspondent pas à nos enregistrements.',
    ])->withInput(); ``` and in my web.php i have this route ``` Route::get('/home', function () {
return view('components.homepage');

}); ``` but when i try to test login with postman i just receive a token with a 200 status meanwhile on the browser the page just reloads whether im putting the right or wrong credentials. I used csrf in the login form (i dont know too much about sessions) i dont really get the behavior. Chatgpt said something about middlewares but i've never used them and couldnt find the folder Middleware. Also i dont know if the fact that i had used jwt and made some configuration so it can be used could be causing the issue although i doubt since it's wokring with api. I'm using laravel 11. Feel free to ask any questions or more clarifications

wind charm
#

Wait, you said you are using an API? And mixing it with views?

#

Middlewares are used, dont really know how to put it, but kinda used as a layer between fromt and backend, lets say you have an Auth middleware, when you have routes defined with it, only users that are authenticated can access that page/route, theres more middlewares besides this, you can check Laravel's docs

azure girder
wind charm
#

Look, when using web it uses a session authentication as you can see, and is managed by Laravels auth facade
What you should do if login is successfull is redirect the hsed to your route /home, otherwise return back with errors as you are already doing.
You dont need postman for this since you arent exposing api for this

sleek musk
#

@azure girder is your topic solved? please mark it with solved then