samedi 15 juillet 2023

Error authenticating user and displaying validation messages in Laravel

I'm facing difficulties with user authentication and displaying validation messages in Laravel. When attempting to log in, the user is not authenticated correctly, and the validation error messages are not displayed.

I followed the official Laravel documentation for authentication and validation, but I'm not getting the expected result even with the help of artificial intelligence. Here are some of my code snippets:

public function store(Request $request)
{
    $request->validate([
        'email' => 'required|email',
        'password' => 'required',
    ]);

    $credentials = $request->only('email', 'password');

    if (auth()->attempt($credentials)) {
        $user = auth()->user();
        $request->session()->put('user', $user);
        return redirect()->route('home');
    }

    throw ValidationException::withMessages([
        'email' => 'Invalid email or password.',
    ]);
}

Here is the HTML code:

    <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Login</title>
</head>
<body>
    <h1>Login</h1>
    <form action="/login" method="post">
    @csrf
        <input type="email" name="email" placeholder="Email">
        <input type="password" name="password" placeholder="Password">
        <input type="submit" value="Login">
    </form>
</body>
</html>

And finally, the routes:

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\UserController;
use App\Http\Controllers\HomeController;
use App\Http\Controllers\LoginController;

Route::get('/users', [UserController::class, 'index'])->name('users.index');
Route::get('/users/create', [UserController::class, 'create']);
Route::post('/users', [UserController::class, 'store']);
Route::get('/users/{user}', [UserController::class, 'show']);
Route::get('/users/{user}/edit', [UserController::class, 'edit']);
Route::put('/users/{user}', [UserController::class, 'update']);
Route::delete('/users/{user}', [UserController::class, 'destroy'])->name('users.destroy');

Route::get('/home', [HomeController::class, 'index'])->name('home');
Route::get('/login', [LoginController::class, 'index'])->name('login');
Route::post('/login', [LoginController::class, 'store']);
?>

I attempted to authenticate the user using the credentials provided in the login form. I expected Laravel to correctly authenticate the user if the credentials were correct. I also set validation rules in the store() method of the LoginController using $request->validate([...]). I expected that if the email and password fields were not filled correctly, Laravel would display the error messages on the login.blade.php view.

When I submit the login form with correct credentials, Laravel doesn't authenticate the user correctly and redirects back to the login page without displaying any error messages. When I submit the form with empty fields or invalid data, the corresponding error messages are also not displayed on the view. Instead, the form is redirected back to the login page without any feedback.

I believe something is missing or configured incorrectly because Laravel is not authenticating the user correctly and not displaying the validation messages.

I would appreciate any help you can provide to solve this issue.



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/VKQS2rH
via IFTTT

Aucun commentaire:

Enregistrer un commentaire