我最近在Laravel 5上启动了一个新项目,我正在尝试实现一个登录功能。
对于username,我使用的代码可以是CPF或CNPJ(巴西个人税务登记号码(CPF)和公司税务登记号码(CNPJ),格式分别为999.999.999-99和99.999.999/9999-99),并用所有点、破折号和斜线保存在数据库中,输入被屏蔽了,所以它们也来自输入的格式(我仔细检查了传递给$username变量的字符串输入,它是正确的)。
问题是Auth::attend()方法总是返回false。我在stackoverflow上找到了所有关于这个问题的答案,但没有一个解决了,所以我要打开另一个问题。
我在Homestead环境和由Homestead提供的MySQL本地数据库上使用了Laravel5.0.16。
这是我的表格:
<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>
这是my 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 protected]',
'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'
]);
}
}
}
提前谢谢。
最佳答案
您的Auth::attempt
正在寻找cpfcnpj
作为用户名,但您已在sqpessoa
方法中将getAuthIdentifier()
指定为用户名字段。
另外,您在cpfcnpj
方法中将getAuthPassword()
指定为密码字段,而Auth::attempt
将cpfcnpj
指定为用户名而不是密码。
关于php - Laravel 5 Auth::attempt()总是返回false,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38274793/