Настройте Аутентификацию в Laravel
Я использую Laravel 5.1, и мне нужно использовать существующую таблицу пользователей, которая имеет собственный алгоритм паролей. После нескольких часов исследований я нашел решение, и вот шаги. Надеюсь, это поможет Ларавелерам.
1 ответ
Решение
В файле config/auth.php установите для драйвера значение custom. как это.
...
'driver' => 'custom',
...
создайте файл 'CustomUserProvider.php в каталоге app/Auth.
<?php
namespace App\Auth;
use App\Model\User;
use Carbon\Carbon;
use Illuminate\Auth\GenericUser;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Contracts\Auth\UserProvider;
class CustomUserProvider implements UserProvider{
/**
* Retrieve a user by their unique identifier.
*
* @param mixed $identifier
* @return \Illuminate\Contracts\Auth\Authenticatable|null
*/
public function retrieveById($identifier){
$qry = User::where('id', '=', $identifier);
if ($qry->count() > 0){
$user = $qry->select('id', 'firstName', 'lastName', 'city', 'state', 'zipcode', 'email', 'dob'
, 'mobilePhone', 'password', 'question', 'answer', 'passwordSalt', 'promoCode', 'createdDate', 'isActivated', 'activationDate'
, 'isDeleted', 'firstTimeLogin', 'role', 'remember_token')->first();
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){
$qry = User::where('id', '=', $identifier)
->where('remember_token', '=', $token);
if ($qry->count() > 0){
$user = $qry->select('id', 'firstName', 'lastName', 'city', 'state', 'zipcode', 'email', 'dob'
, 'mobilePhone', 'password', 'question', 'answer', 'passwordSalt', 'promoCode', 'createdDate', 'isActivated', 'activationDate'
, 'isDeleted', 'firstTimeLogin', 'role', 'remember_token')->first();
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){
$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){
$qry = User::where('email', 'like', $credentials['email']);
if ($qry->count() > 0){
$user = $qry->select('id', 'firstName', 'lastName', 'city', 'state', 'zipcode', 'email', 'dob'
, 'mobilePhone', 'password', 'question', 'answer', 'passwordSalt', 'promoCode', 'createdDate', 'isActivated', 'activationDate'
, 'isDeleted', 'firstTimeLogin', 'role', 'remember_token')->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){
$salt = base64_decode($user->passwordSalt);
$password = $credentials['password'];
$utf16Password = mb_convert_encoding($password, 'UTF-16LE', 'UTF-8');
$calculatedPassword = base64_encode(sha1($salt . $utf16Password, true));
if ($user->email == $credentials['email'] && $user->getAuthPassword() == $calculatedPassword){
return true;
}
return false;
}
}
?>
Затем создайте файл 'CustomAuthProvider.php' в каталоге app/Providers.
<?php
namespace App\Providers;
use App\Auth\CustomUserProvider;
use Illuminate\Support\ServiceProvider;
class CustomAuthProvider extends ServiceProvider{
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot(){
$this->app['auth']->extend('custom', function(){
return new CustomUserProvider();
});
}
/**
* Register the application services.
*
* @return void
*/
public function register(){
//
}
}
?>
Вот и все.