我正在尝试做一个新的Provider
来处理会话,并且我也做了一个新的用户提供程序来处理身份验证。.
我测试了Auth:Attemp()
并可以正常工作,但是当我尝试在另一页中获取Auth::check()
时,如果为false并且userdata为null,则进行身份验证。
我检查并在登录时在会话中创建记录。
这是我的用户模型
<?php
namespace App;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Auth\Authenticatable as AuthenticableTrait;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
class Usuarios extends Model implements Authenticatable
{
use AuthenticableTrait;
protected $primary_key = 'id';
protected $table = 'db_usuarios';
/**
* Get the password for the user.
*
* @return string
*/
public function getAuthID()
{
return $this->id;
}
public function getAuthIdentifierName()
{
return "nombre";
}
/**
* Get the unique identifier for the user.
*
* @return mixed
*/
public function getAuthIdentifier()
{
return $this->{$this->getAuthIdentifierName()};
}
/**
* Get the password for the user.
*
* @return string
*/
public function getAuthPassword()
{
return $this->password;
}
/**
* Get the token value for the "remember me" session.
*
* @return string
*/
public function getRememberToken()
{
if (! empty($this->getRememberTokenName())) {
return $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;
}
<?php
return [
'defaults' => [
'guard' => 'Usuarios',
'passwords' => 'users',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users'
],
'api' => [
'driver' => 'token',
'provider' => 'users',
'hash' => false,
],
'Usuarios' => [
'driver' => 'session',
'provider' => 'customProvider',
],
],
'collectors' => [
'auth' => false, // Display Laravel authentication status
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Usuarios::class,
],
'customProvider' => [
'driver' => 'apiAuthServiceProvider',
'model' => App\Usuarios::class,
],
],
'passwords' => [
'users' => [
'provider' => 'customProvider',
//'table' => 'password_resets',
'expire' => 60,
],
],
];
我的UserGuard自定义
<?php
namespace App\Auth;
use Illuminate\Contracts\Auth;
use Illuminate\Auth\EloquentUserProvider;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\DB;
use Illuminate\Contracts\Auth\Authenticatable as UserContract;
use App\Usuarios;
class UserGuardAuth extends EloquentUserProvider
{
public function retrieveByCredentials(array $credentials)
{
if(strlen($credentials['nombre']) < 1 or strlen($credentials['password']) < 1) {
throw new \Exception("No completaste todos los campos", 1);
}
$hashedPassword = strtoupper(hash('whirlpool', $credentials['password']));
return Usuarios::where('nombre', $credentials['nombre'])->where('password', $hashedPassword)->first();
}
public function validateCredentials(UserContract $user, array $credentials) {
$hashedPassword = strtoupper(hash('whirlpool', $credentials['password']));
$isValid = $credentials['nombre'] == $user->getAuthIdentifier() && $hashedPassword == $user->getAuthPassword();
if( $isValid == true )
{
return true;
}
return false;
}
}
我的身份验证提供程序自定义
<?php
namespace App\Providers;
use App\Auth\UserGuardAuth;
use Auth;
use Illuminate\Contracts\Auth\Access\Gate as GateContract;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
class EloquentUserProvider extends ServiceProvider
{
protected $policies = [
'App\Model' => 'App\Policies\ModelPolicy',
];
public function boot(GateContract $gate)
{
$this->registerPolicies($gate);
Auth::provider('apiAuthServiceProvider', function($app, array $config) {
return new UserGuardAuth( $app['hash'], $config['model'] );
});
}
public function register() {
//
}
}
?>