创建您自己的忽略界面。我在我的项目中采用了它,它运行良好。Guard
UserProvider
PinGuard.php
<?php
declare(strict_types=1);
namespace App\Auth\Guards;
use App\User;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Http\Request;
class PinGuard implements Guard
{
protected $user;
protected $request;
public function __construct(Request $request)
{
$this->request = $request;
}
public function check(): bool
{
return (bool)$this->user();
}
public function guest(): bool
{
return !$this->check();
}
public function id(): ?int
{
$user = $this->user();
return $user->id ?? null;
}
public function setUser(?Authenticatable $user): self
{
$this->user = $user;
return $this;
}
public function validate(array $credentials = []): bool
{
throw new \BadMethodCallException('Unexpected method call');
}
public function authenticate(): User
{
$user = $this->user();
if ($user instanceof User) {
return $user;
}
throw new AuthenticationException();
}
public function user(): ?User
{
return $this->user ?: $this->signInWithPin();
}
protected function signInWithPin(): ?User
{
}
public function logout(): void
{
if ($this->user) {
$this->setUser(null);
}
}
}
NoRememberTokenAuthenticatable.php
User
应该混合这种特征。
<?php
declare(strict_types=1);
namespace App\Auth;
use Illuminate\Database\Eloquent\Model;
trait NoRememberTokenAuthenticatable
{
public function getAuthIdentifierName()
{
return 'id';
}
public function getAuthIdentifier()
{
return $this->id;
}
public function getAuthPassword()
{
throw new \BadMethodCallException('Unexpected method call');
}
public function getRememberToken()
{
throw new \BadMethodCallException('Unexpected method call');
}
public function setRememberToken($value)
{
throw new \BadMethodCallException('Unexpected method call');
}
public function getRememberTokenName()
{
throw new \BadMethodCallException('Unexpected method call');
}
}
AuthServiceProvider.php
<?php
declare(strict_types=1);
namespace App\Providers;
use App\Auth\Guards\PinGuard;
use Illuminate\Container\Container;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Auth;
class AuthServiceProvider extends ServiceProvider
{
protected $policies = [];
public function boot()
{
Auth::extend('pin', function (Container $app) {
return new PinGuard($app['request']);
});
$this->registerPolicies();
}
}
config/auth.php
您应该注释掉其中的大多数。
return [
'defaults' => [
'guard' => 'web',
],
'guards' => [
'web' => [
'driver' => 'pin',
],
],
'providers' => [
],
];