Laravel Login & Registration with Error Code
Would like to have an error message returned when an invalid login email or password is detected..
What was on my mind was to have a controller to trigger the error message to appear on the login.php. is it recommended?
Middleware/Authenticate
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Contracts\Auth\Guard;
class Authenticate
{
/**
* The Guard implementation.
*
* @var Guard
*/
protected $auth;
/**
* Create a new filter instance.
*
* @param Guard $auth
* @return void
*/
public function __construct(Guard $auth)
{
$this->auth = $auth;
}
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if ($this->auth->guest()) {
if ($request->ajax()) {
return response('Unauthorized.', 401);
} else {
return redirect()->guest('login');
}
}
return $next($request);
}
}
Login.php
<!-- resources/views/auth/login.blade.php -->
</style>
<form method="POST" action="login">
{!! csrf_field() !!}
<h1>iMakan | Login</h1>
<div>
Email
<input class="form-control" type="email" name="email" value="{{ old('email') }}">
</div>
<div>
Password
<input class="form-control" type="password" name="password" id="password" >
</div>
<div>
<input type="checkbox" name="remember"> Remember Me
</div>
<div>
<button class="btn btn-lg btn-primary btn-block" type="submit">Login</button>
</div>
<br>
<div>
<p><a href="register">Sign Up</a></p>
</div>
<div>
<p><a href="register">Forget Password</a></p>
</div>
</form>
@stop
You can use this to loop errors