Laravel身份验证无法重定向

时间:2019-04-19 23:23:08

标签: laravel

我正在尝试创建一个身份验证系统来阻止任何未经登录的访问,但是经过身份验证的方法始终会加载登录页面。我重写了AuthenticatesUsers中的所有方法,因为我更改了用户的表名和列,但始终似乎总是加载了登录页面。我可以使用经过身份验证的方法返回视图,但是如果我尝试转到另一个页面,这些视图会将我重定向到登录页面。

我的数据库 enter image description here

我的登录表格

<html>
<head>
    <meta name="csrf-token" content="{{ csrf_token() }}">
</head>
<body>
{{ Form::open(['action' => 'Auth\LoginController@login','methode' => 'POST']) }}
    {{Form::text('login', old('username') ?: old('email'),['class'=>''.$errors->has('email')||$errors->has('username') ? 'is-invalid' : ''.' form-control' ,'placeholder'=>'Nom d\'utilisateur ou email' ,'required', 'autofocus'])}}
    @if ($errors->has('username') || $errors->has('email'))
        <span class="invalid-feedback">
            <strong>{{ $errors->first('username') ?: $errors->first('email') }}</strong>
        </span>
    @endif
        {{Form::password('mot_de_passe', ['class'=>''.$errors->has('password')? 'is-invalid' : ''.' form-control','placeholder'=>'Mot De Passe' , 'required'])}}
        @if ($errors->has('password'))
        <span class="invalid-feedback" role="alert">
            <strong>{{ $errors->first('password') }}</strong>
        </span>
        @endif
        
    <button type="submit" class="btn btn-outline-info btn-block"><i class="ft-unlock"></i>
        {{ __('Login') }}
    </button>
{{ Form::close() }}
</body>
</html>

PagesController

class PagesController extends Controller
{
    protected $redirectTo = '/'; // Redirect after successfull login

    public function __construct()
    {
        $this->middleware('auth');
    }

    public function tableau_bord()
    {
        return view('pages.tableau_bord');
    }
}

在LoginController中登录时使用的方法

 class LoginController extends Controller
 {
 protected $redirectTo = '/';

 public function __construct()
 {
    $this->middleware('guest')->except('logout');
    $this->username = $this->findUsername();
 }
 public function findUsername()
 {
 $login = request()->input('login');

 $fieldType = filter_var($login, FILTER_VALIDATE_EMAIL) ? 'email' : 
 'nom_utilisateur';

 request()->merge([$fieldType => $login]);
 return $fieldType;
}

public function login(Request $request)
{
    $this->validateLogin($request);
    if ($this->hasTooManyLoginAttempts($request)) {
        $this->fireLockoutEvent($request);
        return $this->sendLockoutResponse($request);
    }

    if ($this->attemptLogin($request)) {
        return $this->sendLoginResponse($request);
    }

    $this->incrementLoginAttempts($request);
    return $this->sendFailedLoginResponse($request);
}

protected function validateLogin(Request $request)
{
    $request->validate([
        $this->username => 'required|string',
        'mot_de_passe' => 'required|string',
    ]);
}

protected function attemptLogin(Request $request)
{
    return $this->guard()->attempt(
        array($this->username => $request->login, 'password' =>
            $request > mot_de_passe)
    );
}

protected function sendLoginResponse(Request $request)
{
    $request->session()->regenerate();
    $this->clearLoginAttempts($request);
    return $this->authenticated($request, $this->guard()->user())
        ?: redirect()->intended($this->redirectPath());
}

protected function authenticated(Request $request, $user)
{
    return redirect()->to($this->redirectPath());
}

public function redirectPath()
{
    if (method_exists($this, 'redirectTo')) {
        return $this->redirectTo();
    }
    return property_exists($this, 'redirectTo') ? $this->redirectTo : '/';
}

public function redirectTo()
{
    return $this->redirectTo;
}

protected function sendFailedLoginResponse(Request $request)
{
    throw ValidationException::withMessages([
        $this->username() => [trans('auth.failed')],
    ]);
}

public function username()
{
    return $this->username;
}

protected function credentials(Request $request)
{
    return $request->only($this->username(), 'mot_de_passe');
}
}

1 个答案:

答案 0 :(得分:0)

如果有人遇到此问题,我可以通过将表重命名为用户以及DB和登录表单&loginController&config / auth.php中的email / password字段来解决... 基本上在make:auth命令之后回到了舞台,laravel显然讨厌更改用户表:(