I am trying to make a login page with Laravel request validation method
And Credential verification -> if user is not authenicated it will return an error with 'wrong password ....'
I see errors in two different situation:-
1- When click on login button without filling any thing:-
htmlspecialchars() expects parameter 1 to be string, object given (View: C:\xampp\htdocs
fbwebesources\views\login.blade.php)
2- When fill a random/wrong username and password :
ErrorException in f65ed669c524327dfe53b3286e027354370e4cf5.php line 13:
Call to a member function all() on string (View: C:\xampp\htdocs
fbwebesources\views\login.blade.php)
This is my Login view file:-
<h1>Login Page</h1>
@if (session()->has('errors'))
<div class="alert alert-danger">
<ul>
{{session('errors')}}
</ul>
</div>
@endif
@if (count($errors) > 0)
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
<form action="{{route('dologin')}}" method="post">
{{csrf_field()}}
Username: <input type="text" name="username" autocomplete="off">
<br>
<br>
Password: <input type="password" name="password">
<br>
<button>Login</button>
</form>
This is my login controller :
public function doLogin(Request $request)
{
$this->validate($request, [
'username' => 'required|max:255',
'password' => 'required',
]);
$client = new GuzzleHttp\Client(['base_uri' => 'https://domainname/api/v1/']);
try {
$response = $client->request('POST', 'login', [
'form_params' => [
'username' => $request->username,
'password' => $request->password,
],
]);
}
catch (ClientException $exception) {
return back()->with('errors', 'Invalid username and/or password');
}
}
I removed the authentication error message in view file
@if (session()->has('errors'))
<div class="alert alert-danger">
<ul>
{{session('errors')}}
</ul>
</div>
@endif
The laravel request validator worked fine .
When i removed the validator error message in view file, the authenication error message worked !
Is there any conflict i made in both error message ?
How can i make both of them work
Solved by changing the following :- session('errors') to session('error')
there was a conflict in names of laravel validator session name , and the session name i set to return the authentication error message