Я добавляю следующие коды в файл config / auth.php
<?php
'guards' => [
'usercustom' => [
'driver' => 'session',
'provider' => 'userCustomProvider',
],
],
'providers' => [
'userCustomProvider' => [
'driver' => 'eloquent',
'model' => App\Models\UserCustom::class,
],
],
?>
Затем я сделал два класса CustomAuthProvider и CustomUserProvider в floder провайдеров
<?php
namespace App\Providers;
use Illuminate\Support\Facades\Auth;
use App\Providers\CustomUserProvider;
use Illuminate\Support\ServiceProvider;
class CustomAuthProvider extends ServiceProvider {
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
$this->app['auth']->extend('usercustom',function()
{
return new CustomUserProvider();
});
}
/**
* Register the application services.
*
* @return void
*/
public function register()
{
//
}
}
<?php
namespace App\Providers;
use App\Models\UserCustom;
use Carbon\Carbon;
use Illuminate\Auth\GenericUser;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Contracts\Auth\UserProvider;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Log;
class CustomUserProvider implements UserProvider {
/**
* Retrieve a user by their unique identifier.
*
* @param mixed $identifier
* @return \Illuminate\Contracts\Auth\Authenticatable|null
*/
public function retrieveById($identifier)
{
// TODO: Implement retrieveById() method.
$qry = UserCustom::where('usr_user_id','=',$identifier);
if($qry->count() >0)
{
$user = $qry->select('usr_user_id', 'usr_firstname', 'usr_last_name', 'usr_email', 'usr_pass')->first();
$attributes = array(
'usr_user_id' => $user->usr_user_id,
'usr_firstname' => $user->usr_firstname,
'usr_last_name' => $user->usr_last_name,
'usr_email' => $user->usr_email,
'usr_pass' => $user->usr_pass,
);
return $user;
}
return null;
}
/**
* Retrieve a user by by their unique identifier and "remember me" token.
*
* @param mixed $identifier
* @param string $token
* @return \Illuminate\Contracts\Auth\Authenticatable|null
*/
public function retrieveByToken($identifier, $token)
{
// TODO: Implement retrieveByToken() method.
$qry = UserCustom::where('usr_user_id','=',$identifier);
if($qry->count() >0)
{
$user = $qry->select('usr_user_id', 'usr_firstname', 'usr_last_name', 'usr_email', 'usr_pass')->first();
$attributes = array(
'usr_user_id' => $user->usr_user_id,
'usr_firstname' => $user->usr_firstname,
'usr_last_name' => $user->usr_last_name,
'usr_email' => $user->usr_email,
'usr_pass' => $user->usr_pass,
);
return $user;
}
return null;
}
/**
* Update the "remember me" token for the given user in storage.
*
* @param \Illuminate\Contracts\Auth\Authenticatable $user
* @param string $token
* @return void
*/
public function updateRememberToken(Authenticatable $user, $token)
{
// TODO: Implement updateRememberToken() method.
$user->setRememberToken($token);
$user->save();
}
/**
* Retrieve a user by the given credentials.
*
* @param array $credentials
* @return \Illuminate\Contracts\Auth\Authenticatable|null
*/
public function retrieveByCredentials(array $credentials)
{
// TODO: Implement retrieveByCredentials() method.
$qry = UserCustom::where('usr_email','=',$credentials['usr_email']);
if($qry->count() > 0)
{
$user = $qry->select('usr_user_id','usr_email','usr_pass')->first();
return $user;
}
return null;
}
/**
* Validate a user against the given credentials.
*
* @param \Illuminate\Contracts\Auth\Authenticatable $user
* @param array $credentials
* @return bool
*/
public function validateCredentials(Authenticatable $user, array $credentials)
{
// TODO: Implement validateCredentials() method.
// we'll assume if a user was retrieved, it's good
// DIFFERENT THAN ORIGINAL ANSWER
if($user->email == $credentials['usr_email'] && Hash::check($credentials['usr_pass'], $user->getAuthPassword()))//$user->getAuthPassword() == md5($credentials['password'].\Config::get('constants.SALT')))
{
//$user->last_login_time = Carbon::now();
$user->save();
return true;
}
return false;
}
}
Затем я сделал два маршрута
Route::get('user/login','UserController@loginForm');
Route::post('user/login','UserController@loginCheck');
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
class UserController extends Controller
{
public function loginForm () {
return view('login');
}
public function loginCheck (Request $request) {
$credentials=array([
'usr_email'=> $request->email,
'usr_pass'=>$request->password
]);
if (Auth::guard('usercustom')->attempt($credentials)){
echo "1";
}else {
echo "0";
}
}
}
Я написал модель класса в начале темы. Когда достиг Auth :: guard, появилась следующая ошибка
QLSTATE [42S22]: столбец не найден:1054 Неизвестный столбец '0' в 'предложении where' (SQL: выберите * из tua_users
, где 0
в (test@test.com, 123456) предел 1) Предыдущие исключения SQLSTATE [42S22]: Столбец не найден: 1054 Неизвестностолбец '0' в 'предложении where' (42S22)
Я слишком много ищу в Интернете, но не могу решить проблему.Проблема в полях таблицы. Laravel по умолчанию требует электронную почту и пароль, в то время как я изменил их на «usr_email» и «usr_pass». А также я изменил таблицу и модель