I just checked using dd() that my variable $username and $password are not null.
But why Auth::attempt($user) is always return error message 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');
}
}