首页 > 解决方案 > “未定义索引:密码” Laravel 8.61.0

问题描述

我刚刚检查了使用dd()我的变量$username并且$password不为空。但是为什么Auth::attempt($user)总是返回错误信息Undefined index: password

登录控制器.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');
    }
}

用户.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');
    }
}

标签: phplaravel

解决方案


我认为你应该创建一个这样的数组

if (Auth::attempt(['email' => $email, 'password' => $password, 'active' => 1])) {
    // Authentication was successful...
}

您需要将用户名更改为电子邮件

然后 password_hash 到密码,你需要改变你的数据库用户表。

https://laravel.com/docs/8.x/authentication#authenticating-users


推荐阅读