[英]Error: Login attribute is required using Sentry and Laravel
從今天開始,我的登錄控制器(使用Sentry)不再起作用。 我已經將其更改了10203902次,但它一直給我錯誤:“需要[登錄]屬性”。 在過去的2-3周中,該控制器運行良好。
的LoginController
<?php namespace Digitus\Auth\Controllers;
use Illuminate\View\Environment as View;
use Cartalyst\Sentry\Sentry;
use Illuminate\Support\Facades\Input;
use Illuminate\Routing\Redirector as Redirect;
class LoginController extends \Digitus\Base\Controllers\BController{
public function __construct(Input $input, View $view, Sentry $sentry, Redirect $redirect){
$this->input = $input;
$this->view = $view;
$this->sentry = $sentry;
$this->user = $this->sentry->getUser();
$this->redirect = $redirect;
if($this->sentry->check()) {
$user = $this->sentry->getUser();
} else {
$user = null;
}
$this->view->share('loggedUser', $user);
}
protected $input;
protected $view;
protected $sentry;
protected $redirect;
public function index()
{
return $this->view->make('user.login');
}
public function login()
{
$user = $this->user->where('username', $this->input->get('email_or_username'))->orWhere('email', $this->input->get('email_or_username'))->first();
$credentials = array(
'email' => Input::get('email_or_username') ?: null,
'password' => $this->input->get('password'),
);
try {
$user = $this->sentry->authenticate($credentials, false);
if($this->sentry->check())
{
return $this->redirect->to('admin');
} else {
return $this->redirect->to('');
}
}
catch (\Exception $e)
{
return $this->redirect->to('login')->withErrors(array('login'=> $e->getMessage()));
}
}
}
視圖
@extends('layouts.main')
@section('content')
<div class="col-md-4 col-md-offset-4">
<div class="panel panel-info">
<div class="panel-heading">PLease Login</div>
<div class="panel-body">
{{ Form::open(array('url' => 'login')) }}
@if($errors->has('login'))
<div class="alert alert-danger">
<a href="#" class="close" data-dismiss="alert">×</a>
{{ $errors->first('login', ':message') }}
</div>
@endif
<div class="form-group">
{{ Form::label('email_or_username', 'Email Address or Username') }}
{{ Form::email('email_or_username', '', array('class'=>'form-control','placeholder'=>'Please enter your Email Address or Username to login', 'autofocus')) }}
</div>
<div class="form-group">
{{ Form::label('password', 'Password') }}
{{ Form::password('password', array('class'=>'form-control', 'placeholder'=>'Password')) }}
</div>
<div class="form-group">
{{ Form::submit('Login', array('class'=>'btn btn-success')) }}
{{ HTML::link('/', 'Cancel', array('class'=>'btn btn-danger')) }}
</div>
<hr>
<div class="form-group">
<h2>Don't have an account?</h2>
{{ HTML::link('register', 'Please register here', array('class'=>'btn btn-primary btn-xs'))}}
</div>
{{ Form::close() }}
</div>
</div>
</div>
@stop
我已經問過很多人尋求解決方案或修復等。他們都沒有用。
PS&僅供參考:我還發布了Sentry配置,login_attribute是電子郵件。 任何幫助或投入都會很棒。 提前致謝..
哨兵也使用
protected static $loginAttribute = 'email';
要獲取登錄數據,請檢查是否已對其進行更改。 這些是給您帶來困難的路線:
$loginName = $this->userProvider->getEmptyUser()->getLoginName();
$loginCredentialKey = (isset($credentials[$loginName])) ? $loginName : 'login';
基本上,如果$loginAttribute
不可用,它將使用'login'作為憑據密鑰。 因此,您也可以測試以提供所需的內容:
$credentials = array(
'email' => Input::get('email_or_username') ?: null,
'login' => Input::get('email_or_username') ?: null,
'password' => $this->input->get('password'),
);
不幸的是,您不能有兩個不同的字段來登錄用戶,因此,如果需要此操作,則必須手動進行檢查,因此您可能可以執行以下操作:
public function login()
{
$user = $this->sentry->getUserProvider()->getEmptyUser();
$user = $user->where('email', $this->input->get('email_or_username'))
->orWhere('username', $this->input->get('email_or_username'))
->first();
try
{
if ($user && Hash::check($this->input->get('password'), $user->password)) /// here you check if a user was found
{
$this->sentry->login($user, false); /// remember = false /// here is the login happening in case of true
}
else
{
// user not found or wrong password
}
}
catch (\Exception $e)
{
return $this->redirect->to('login')->withErrors(array('login'=> $e->getMessage()));
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.