I need change column password name from 'password' to 'user_password'. I don't know what is wrong in my code because after login nothing to do, redirect me to login form again. Below my code: User model:
<?php
namespace App\Models;
// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
use Illuminate\Support\Facades\Hash;
class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'user_firstname',
'user_lastname',
'user_email',
'user_password',
];
/**
* The attributes that should be hidden for serialization.
*
* @var array<int, string>
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
public function getAuthPassword() {
return $this->user_password;
}
}
LoginController:
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
class LoginController extends Controller {
use AuthenticatesUsers;
/**
* Where to redirect users after login.
*
* @var string
*/
// protected $redirectTo = RouteServiceProvider::HOME;
protected $redirectTo = 'admin';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct() {
$this->middleware('guest')->except('logout');
}
public function username(): string {
return 'user_email';
}
protected function validateLogin(Request $request) {
$this->validate($request, [
$this->username() => 'required', 'user_password' => 'required',
]);
}
public function login(Request $request){
$this->validateLogin($request);
if ($this->attemptLogin($request)) {
if ($request->hasSession()) {
$request->session()->put('auth.password_confirmed_at', time());
}
return $this->sendLoginResponse($request);
}
$this->incrementLoginAttempts($request);
return $this->sendFailedLoginResponse($request);
}
protected function credentials(Request $request) {
return $request->only($this->username(), 'user_password');
}
public function showLoginForm() {
return view('admin.login.login');
}
}
CustomUserProvider:
<?php
namespace App\Providers;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Contracts\Auth\UserProvider as UserProviderContract;
use Illuminate\Contracts\Hashing\Hasher as HasherContract;
use Illuminate\Contracts\Auth\Authenticatable as UserContract;
class CustomUserProvider implements UserProviderContract {
protected $model;
protected $hasher;
/**
* The callback that may modify the user retrieval queries.
*
* @var (\Closure(\Illuminate\Database\Eloquent\Builder):mixed)|null
*/
protected $queryCallback;
public function __construct(HasherContract $hasher, $model) {
$this->model = $model;
$this->hasher = $hasher;
}
public function retrieveByCredentials(array $credentials) {
$credentials = array_filter(
$credentials,
fn($key) => !str_contains($key, 'user_password'),
ARRAY_FILTER_USE_KEY
);
if (empty($credentials)) {
return;
}
// First we will add each credential element to the query as a where clause.
// Then we can execute the query and, if we found a user, return it in a
// Eloquent User "model" that will be utilized by the Guard instances.
$query = $this->newModelQuery();
foreach ($credentials as $key => $value) {
if (is_array($value) || $value instanceof Arrayable) {
$query->whereIn($key, $value);
} elseif ($value instanceof Closure) {
$value($query);
} else {
$query->where($key, $value);
}
}
return $query->first();
}
protected function newModelQuery($model = null) {
$query = is_null($model) ? $this->createModel()->newQuery() : $model->newQuery();
with($query, $this->queryCallback);
return $query;
}
public function createModel() {
$class = '\\' . ltrim($this->model, '\\');
return new $class;
}
public function retrieveById($identifier) {
}
public function retrieveByToken($identifier, $token) {
}
public function updateRememberToken(Authenticatable $user, $token): void {
}
public function validateCredentials(UserContract $user, array $credentials) {
if (is_null($plain = $credentials['user_password'])) {
return false;
}
return $this->hasher->check($plain, $user->getAuthPassword());
}
}
config/Auth.php:
'providers' => [
'users' => [
'driver' => 'custom',
'model' => App\Models\User::class,
],
Please tell me how can I login with changed columns names in User table.