Laravel用户无法登录,只有管理员可以登录

时间:2018-09-07 18:21:02

标签: laravel

在laravel中,我可以使用管理员帐户登录没有问题,但是当我使用任何用户帐户登录时,它没有登录并且没有给出任何错误,只有登录页面会刷新,并且多数民众赞成在没有给出任何错误的情况下,是的,我检查了Chrome检查并HttpRequest并没有任何反应, 还检查MySQL数据库没有问题,也不会给出错误。

LoginController

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;

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 = '/home';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }
}

RedirectIfAuthenticated

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;

class RedirectIfAuthenticated
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @param  string|null  $guard
     * @return mixed
     */
    public function handle($request, Closure $next, $guard = null)
    {
        if (Auth::guard($guard)->check()) {
            return redirect('/home');
        }

        return $next($request);
    }
}

这是另一个LoginController

LoginController类扩展了控制器{

private $Administrator;

function __construct(Administrator $administrator) {
    $this->Administrator = $administrator;
}

public function index(){
    if(Auth::check()) return redirect('/dashboard');

    return view('index.login');
}

public function login(Request $request){

    if(Auth::check()) redirect('/dashboard');

    $Username = $request->input('username');
    $Password = $request->input('password');

    $Admin = $this->Administrator->checkAuthenticate($Username,$Password);

    if($Admin){
        Auth::loginUsingId($Admin->id, true);
        return redirect('/dashboard');
    }

    return redirect('/login');
}

public function logout(){

    if(Auth::check()){
        Auth::logout();
    }

    return redirect('/login');
}

}

1 个答案:

答案 0 :(得分:0)

if(Auth::check()) redirect('/dashboard');

这是普通用户将通过的唯一身份验证检查,并且您不返回重定向,因此普通用户继续执行其余功能,在该功能中他们将被检查以查看自己是否是管理员。他们不是。