Laravel Login & Registration with Error Code

2019-08-11 00:24发布

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

1条回答
劳资没心,怎么记你
2楼-- · 2019-08-11 00:33

You can use this to loop errors

@if(count($errors) > 0)
         <div class="alert alert-danger">
            <strong>Whoops!</strong> There were some problems with your input.<br><br>
            <ul>
                @foreach ($errors->all() as $error)
                    <li>{{ $error }}</li>
                @endforeach
            </ul>
         </div>
    @endif
查看更多
登录 后发表回答