"can"中间件在Laravel中不适用于多个域名。

huangapple go评论54阅读模式
英文:

"can" middleware doesn't work for multi-domains in laravel

问题

I declared a "Gate" like this:

Gate::define('isAdmin', fn (\App\Models\User $user) => true);

in "AuthServiceProvider" and called that like this:

//dashboard subdomains injection
Route::middleware(['web', 'auth', 'can:isAdmin'])
    ->domain('dashboard.khassepaz.test')
    ->group(base_path('routes/dashboard.php'));

//blog subdomains injection
Route::middleware('web')
    ->domain('blog.khassepaz.test')
    ->group(base_path('routes/blog.php'));

//normal domains
Route::middleware('web')
    ->domain('khassepaz.test')
    ->group(base_path('routes/web.php'));

but when I try to go to "dashboard.khassepaz.test," it redirects to the login page since I have already logged in, I am redirected to the home page that is defined in "RouteServiceProvider" in the "HOME" constant. http://khassepaz.test/

where is my mistake? please help.

NOTE: For information, the user has already registered

UPDATE: public const HOME = '/';

英文:

I declared a "Gate" like this:

Gate::define('isAdmin', fn (\App\Models\User $user) => true);

in "AuthServiceProvider" and called that like this:

        //dashboard subdomains injection
    Route::middleware(['web', 'auth', 'can:isAdmin'])  <----------------
        ->domain('dashboard.khassepaz.test')
        ->group(base_path('routes/dashboard.php'));

    //blog subdomains injection
    Route::middleware('web')
        ->domain('blog.khassepaz.test')
        ->group(base_path('routes/blog.php'));

    //normal domains
    Route::middleware('web')
        ->domain('khassepaz.test')
        ->group(base_path('routes/web.php'));

but when I try to go to "dashboard.khassepaz.test", it redirects to the login page since I have already logged in, I am redirected to the home page that is defined in "RouteServiceProvider" in the "HOME" constant. http://khassepaz.test/

where is my mistake? please help.

NOTE: For information, the user has already registered

UPDATE: public const HOME = '/';

答案1

得分: 0

前往 config/session.php 并更改

'domain' => env('SESSION_DOMAIN'),

'domain' => env('SESSION_DOMAIN', '.maindomain.com'),

现在认证用户在主域和所有子域上都可以识别。

英文:

Go to config/session.php and change

'domain' => env('SESSION_DOMAIN'),

to

'domain' => env('SESSION_DOMAIN', '.maindomain.com'),

now the authenticated user is identified over the main domain and all over subdomains.

huangapple
  • 本文由 发表于 2023年6月15日 17:14:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76480954.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定