i have a problem in showing error for users this is my Auth Login Controller :
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
class LoginController extends Controller
{
/*
|--------------------------------------------------------------------------
| Login Controller
|--------------------------------------------------------------------------
|
| This controller handles authenticating users for the application and
| redirecting them to your home screen. The controller uses a trait
| to conveniently provide its functionality to your applications.
|
*/
use AuthenticatesUsers;
/**
* Where to redirect users after login.
*
* @var string
*/
protected $redirectTo = RouteServiceProvider::HOME;
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest')->except('logout');
}
public function login(Request $request)
{
$input = $request->all();
$this->validate($request, [
'email' => 'required|email',
'password' => 'required|min:8',
]);
if(auth()->attempt(array('email' => $input['email'], 'password' => $input['password'])))
{
if (auth()->user()->type == 'admin') {
return redirect()->route('admin.home');
}else if (auth()->user()->type == 'manager') {
return redirect()->route('manager.home');
}else{
return redirect()->route('home');
}
}else{
return redirect()->route('login')
->with('error','Email-Address And Password Are Wrong.');
}
$messages = [
'email.required' => 'Email is required',
'email.email' => 'Invalid email format',
'password.required' => 'Password is required',
'password.min' => 'Password must be at least 8 characters',
];
}
}
This is the LoginController from the default app in laravel i don't know why the messages of password wrong can't be showed in screen when i type wrong pass
i want to show users error if they use wrong password and this is the github link : https://github.com/ElGrandeAchraf/skipshiftProjectCode.git
And Thank you For your attention.
CodePudding user response:
It appears that the error message is not being displayed because it is being set on the redirect rather than being passed back to the view. In the else block of the login method, try replacing
return redirect()->route('login')->with('error','Email-Address And Password Are Wrong.');
With
return back()->withInput()->withErrors(['email' => 'Email-Address And Password Are Wrong.']);
This will pass the error message back to the view so it can be displayed.
Also, you can use $messages
validation rules to show custom error messages, and you should call it before validate function like this:
$messages = [
'email.required' => 'Email is required',
'email.email' => 'Invalid email format',
'password.required' => 'Password is required',
'password.min' => 'Password must be at least 8 characters',
];
$this->validate($request, [
'email' => 'required|email',
'password' => 'required|min:8',
], $messages);
Please note that this is just one possible solution and that it may not be the only problem you're facing in your code. You should also check the routes, views, and other related files, and make sure that everything is set up correctly.
CodePudding user response:
Not sure if I was looking at the right Blade file, but in your resources/views/login.blade.php
file I haven't seen anywhere that you would actually have the code to show errors. The controller doesn't do that for you automatically.
I you are returning with('error')
then you should show it in Blade, something like:
@if (session('error'))
<div >{{ session('error') }}</div>
@endif