【发布时间】:2021-09-24 15:06:08
【问题描述】:
我刚刚使用dd() 检查我的变量$username 和$password 不为空。
但是为什么Auth::attempt($user)总是返回错误信息Undefined index: password
LoginController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class LoginController extends Controller
{
public function index()
{
return view('login/index', [
'title' => 'LOGIN'
]);
}
public function authenticate(Request $request)
{
$hashed_password = hash(config('var.default_hash'), $request['password']);
$request['password'] = $hashed_password;
$request->validate([
'username' => ['required'],
'password' => ['required'],
]);
$user = [
'username' => $request['username'],
'password_hash' => $request['password']
];
if (Auth::attempt($user)) {
$request->session()->regenerate();
return redirect()->intended('/');
}
return back()->with('fail', 'Login fail');
}
}
User.php
<?php
namespace App\Models;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class User extends Model implements Authenticatable
{
use HasFactory;
public $timestamps = false;
protected $fillable = ['name', 'username', 'password_hash', 'lang'];
/**
* The column name of the "remember me" token.
*
* @var string
*/
protected $rememberTokenName = 'remember_token';
/**
* Get the name of the unique identifier for the user.
*
* @return string
*/
public function getAuthIdentifierName()
{
return $this->getKeyName();
}
/**
* Get the unique identifier for the user.
*
* @return mixed
*/
public function getAuthIdentifier()
{
return $this->{$this->getAuthIdentifierName()};
}
/**
* Get the unique broadcast identifier for the user.
*
* @return mixed
*/
public function getAuthIdentifierForBroadcasting()
{
return $this->getAuthIdentifier();
}
/**
* Get the password for the user.
*
* @return string
*/
public function getAuthPassword()
{
return $this->password_hash;
}
/**
* Get the token value for the "remember me" session.
*
* @return string|null
*/
public function getRememberToken()
{
if (!empty($this->getRememberTokenName())) {
return (string) $this->{$this->getRememberTokenName()};
}
}
/**
* Set the token value for the "remember me" session.
*
* @param string $value
* @return void
*/
public function setRememberToken($value)
{
if (!empty($this->getRememberTokenName())) {
$this->{$this->getRememberTokenName()} = $value;
}
}
/**
* Get the column name for the "remember me" token.
*
* @return string
*/
public function getRememberTokenName()
{
return $this->rememberTokenName;
}
}
2021_09_24_081907_create_users_table.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('username');
$table->text('password_hash');
$table->String('lang');
$table->timestamp('created_at')->useCurrent();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
【问题讨论】:
-
你需要发送
password,而不是password_hash,到Auth::attempt -
如果我将
password_hash更改为password,它总是返回false -
默认情况下,Laravel 期望密码字段命名为
password。如果您想改用password_hash,请参阅stackoverflow.com/questions/39374472/…,了解如何更改默认字段。 -
根据link 您不应该对传入请求的密码值进行哈希处理,因为框架会在将其与数据库中的哈希密码进行比较之前自动对该值进行哈希处理。 我的其他错误是在将密码拟合到
Auth之前对密码进行哈希处理。我应该将它们保留为原始文本。 -
没错。存储哈希,但将明文发送给身份验证者。它将使用算法为您检查。