我最近在Laravel 5上开始了一个新项目,我正在尝试实现登录功能。
对于用户名,我使用的代号是CPF或CNPJ(个人(CPF)和公司(CNPJ)的巴西税务登记号,格式为999.999.999-99和99.999.999 / 9999-99分别保存在数据库上,包含所有点,短划线和斜线,并且输入被屏蔽,因此它们也从输入格式化(我仔细检查了传递给$ username变量的字符串输入,这是正确的。)
问题是Auth :: attempt()方法总是返回false。我尝试了在stackoverflow上找到的关于这个问题的所有答案,没有一个解决它,所以我打开了另一个问题。
我在Homestead环境中使用Laravel 5.0.16,在Homestead提供MySQL本地数据库。
这是我的表格:
<form id="login-form" class="contact-form" name="contact-form" method="post"
action="/auth/login">
<div>
<div class="form-group">
<div class="form-group">
<label>CPF/CNPJ</label>
<input type="text" name="username" class="form-control cpfcnpj">
</div>
<div class="form-group">
<label>Senha</label>
<input type="password" name="password" class="form-control">
</div>
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="form-group" style="margin-top: 30px">
<button type="submit" name="submit" class="btn btn-primary btn-lg">
Entrar
</button>
</div>
</div>
</div>
</form>
这是我的User.php:
namespace App;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
class User extends Model implements AuthenticatableContract, CanResetPasswordContract {
use Authenticatable, CanResetPassword;
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'PESSOA';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['sqpessoa', 'cpfcnpj', 'password', 'nome', 'email', 'telefone', 'contato', 'tipo'];
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = ['password', 'remember_token'];
/**
*
* @var type
*/
protected $primaryKey = 'sqpessoa';
/**
*
* @var type
*/
public $timestamps = false;
/**
* Get the unique identifier for the user.
*
* @return mixed
*/
public function getAuthIdentifier() {
return $this->attributes['sqpessoa'];
}
public function getAuthPassword()
{
return $this->cpfcnpj;
}
}
我的AuthController:
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Contracts\Auth\Registrar;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
class AuthController extends Controller {
/*
|--------------------------------------------------------------------------
| Registration & Login Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users, as well as the
| authentication of existing users. By default, this controller uses
| a simple trait to add these behaviors. Why don't you explore it?
|
*/
use AuthenticatesAndRegistersUsers;
/**
* Create a new authentication controller instance.
*
* @param \Illuminate\Contracts\Auth\Guard $auth
* @param \Illuminate\Contracts\Auth\Registrar $registrar
* @return void
*/
public function __construct(Guard $auth, Registrar $registrar) {
$this->auth = $auth;
$this->registrar = $registrar;
$this->middleware('guest', ['except' => 'getLogout']);
}
/**
*
* @param \Illuminate\Http\Request $request
* @return type
*/
public function postLogin(\Illuminate\Http\Request $request) {
$this->validate($request, array('username' => 'required', 'password' => 'required'));
$username = $request->input('username');
$password = $request->input('password');
if (\Auth::attempt(['cpfcnpj' => $username, 'password' => $password])) {
return redirect()->intended($this->redirectPath());
}
return redirect($this->loginPath())->withInput($request->only('username', 'remember'))->withErrors(array('username' => 'Credenciais inválidas.'));
}
}
我的用户迁移(名为'pessoa')数据库:
class CreatePessoaTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('PESSOA', function (Blueprint $table) {
$table->increments('SQPESSOA');
$table->string('CPFCNPJ', 20)->unique();
$table->string('PASSWORD', 255);
$table->string('NOME', 200);
$table->string('EMAIL', 200);
$table->string('TELEFONE', 15);
$table->string('CONTATO', 200)->nullable();
$table->enum('TIPO', ['avulso', 'mensalista', 'patio']);
$table->rememberToken();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('PESSOA');
}
}
种子:
class PessoaTableSeeder extends Seeder
{
public function run()
{
DB::table('PESSOA')->truncate();
$faker = Faker\Factory::create('pt_BR');
DB::table('PESSOA')->insert([
'cpfcnpj' => $faker->cnpj,
'password' => bcrypt('test'),
'nome' => 'Admin',
'email' => 'email@gmail.com',
'telefone' => $faker->phoneNumber,
'contato' => str_random(),
'tipo' => 'patio'
]);
for ($i = 0; $i < 10; $i++) {
DB::table('PESSOA')->insert([
'cpfcnpj' => $faker->unique()->cpf,
'password' => bcrypt($faker->password),
'nome' => $faker->name,
'email' => $faker->email,
'telefone' => $faker->phoneNumber,
'contato' => str_random(),
'tipo' => 'avulso'
]);
}
for ($i = 0; $i < 3; $i++) {
DB::table('PESSOA')->insert([
'cpfcnpj' => $faker->unique()->cnpj,
'password' => bcrypt($faker->password),
'nome' => $faker->company,
'email' => $faker->companyEmail,
'telefone' => $faker->phoneNumber,
'contato' => str_random(),
'tipo' => 'mensalista'
]);
}
}
}
提前致谢。
答案 0 :(得分:2)
您的Auth::attempt
正在寻找cpfcnpj
作为用户名,但您已将sqpessoa
指定为getAuthIdentifier()
方法的用户名字段。
此外,您已将cpfcnpj
指定为getAuthPassword()
方法的密码字段,而Auth::attempt
将cpfcnpj
作为用户名而不是密码。
答案 1 :(得分:1)
将 cpfcnpj 列重新,将密码重命名为数据库中的小写和数组声明
<强> R2:强>
跟进Attempt方法,你可以看到在很多地方它正在等待小写的“密码”字段,问题只出在这个字段上。 CPFCNPJ可能是大写的。