自定义身份验证名称函数-Laravel



我有多身份验证流明应用程序设置,其中一个用于业务,另一个用于典型用户。当我被认证为企业时,我想说:

auth((->business((->id

而不是:

auth((->user((->id

创建一个新的类

<?php
namespace IlluminateSupportFacades;
use IlluminateSupportFacadesAuth;
class CustomAuth extends Auth
{
public static function company()
{
return $this->user()->business;
//I'm not shure, but some like that
}
}

和更改config/app.php

'aliases' => [
'App' => IlluminateSupportFacadesApp::class,
'Artisan' => IlluminateSupportFacadesArtisan::class,
//'Auth' => IlluminateSupportFacadesAuth::class,
'Auth' => IlluminateSupportFacadesCustomAuth::class,

你可以在拉丁美洲阅读更多。

如果您使用的是;web";则使用CCD_ 2类作为保护。您可以扩展如下:

class MySessionGuard extends SessionGuard {

private function isAuthenticatedAsBusiness($user) {
// do your check here
return false; // or true accordingly


public function user() {
$user = parent::user();
return !$this->isAuthenticatedAsBusiness($user) ? $user : null;
}
public function business() {
$user = parent::user();
return $this->isAuthenticatedAsBusiness($user) ? $user : null;
}
}

然后在您的AuthServiceProvider:中注册此防护

public function boot() {
Auth::extend("myweb", function ($app,$name, array $config) {
$guard = new MySessionGuard(
$name, 
Auth::createUserProvider($config['provider']),
$app["session.store"],
$app["request"]
);
// This is copied from https://github.com/laravel/framework/blob/43bea00fd27c76c01fd009e46725a54885f4d2a5/src/Illuminate/Auth/AuthManager.php#L121
if (method_exists($guard, 'setDispatcher')) {
$guard->setDispatcher($this->app['events']);
}
if (method_exists($guard, 'setCookieJar')) {
$guard->setCookieJar($this->app['cookie']);
}
return $guard;
});
// ...

然后,您可以更新config/auth.php

'guards' => [
'web' => [
'driver' => 'myweb',
'provider' => 'users',
],
// ...
],

您的默认防护现在应该是您的自定义防护。

最新更新