search

Home  >  Q&A  >  body text

Problems authenticating users and displaying verification messages in Laravel

<p>I'm having trouble authenticating users and displaying validation messages in Laravel. When trying to log in, the user is not authenticated correctly and the authentication error message is not displayed. </p> <p>I followed the official Laravel documentation for authentication and verification, but even with the help of artificial intelligence, I didn't get the expected results. Here are some snippets of my code: </p> <pre class="brush:php;toolbar:false;">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.', ]); }</pre> <p>This is the HTML code: </p> <pre class="brush:php;toolbar:false;"><!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></pre> <p>Finally, the route: </p> <pre class="brush:php;toolbar:false;"><?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']); ?></pre> <p>I am trying to authenticate the user using the credentials provided in the login form. If the credentials are correct, I expect Laravel to authenticate the user correctly. I also use <code>$request->validate([...])</code> in the <code>store()</code> method of <code>LoginController</code> Validation rules are set.I expect that if the email and password fields are not filled in correctly, Laravel will display an error message on the <code>login.blade.php</code> view. </p> <p>When I submit the login form with the correct credentials, Laravel fails to authenticate the user correctly and redirects back to the login page without showing any error message. When I submit the form with empty fields or invalid data, the corresponding error message is also not displayed on the view. Instead, the form redirects back to the login page without any feedback. </p> <p>I believe that due to something missing or incorrectly configured, Laravel is not properly authenticating the user and displaying the verification message. </p> <p>I would be very grateful for any help you can provide to resolve this issue. </p>
P粉071626364P粉071626364451 days ago689

reply all(1)I'll reply

  • P粉502608799

    P粉5026087992023-09-05 00:46:08

    You initially verified your email and password, which is good. After failed login you threw an exception that's why it doesn't redirect you to the login page. So it should redirect to previous page with error.

    Therefore, modify the store method as follows:

    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');
        }
    
        return redirect()->back()->withErrors(['msg' => '使用的电子邮件或密码无效。']);
    
    }

    Now, use the following code in the blade file of the login page, this will print the error. Modify design or other content as needed

    @if($errors->any())
       <h4>{{$errors->first()}}</h4>
    @endif

    reply
    0
  • Cancelreply