【问题标题】:How to customization redirection in laravel jetstream如何在 laravel jetstream 中自定义重定向
【发布时间】:2021-01-11 09:21:43
【问题描述】:

我运行 laravel jetstream 并设置为 livewire 而不是 vue 。我通常使用 auth ui 并自定义登录

App -> Http -> 控制器 -> auth -> LoginController

在这个 LoginController 中,我像这样自定义这个重定向

 protected function authenticated(Request $request, $user)
{
    if ( $user->isUser() ) {// do your margic here
        return redirect()->route('user_dashboard');
    }
    elseif ($user->isSarpras()) {
        return redirect()->route('admin_sarpras_dashboard');
    }
}

但是在 laravel jetstream iam 上找不到 Controller->auth 。如何使用 laravel jetstream 最好地管理登录和创建多个登录?

【问题讨论】:

  • 是否有任何答案解决了您的问题?如果是,请将它们标记为已接受。

标签: laravel jetstream


【解决方案1】:

Jetstream 使用 Fortify 进行身份验证。

目前,Fortify 无法自定义重定向。

这里有一个请求此行为的未解决问题:https://github.com/laravel/fortify/issues/77

很有可能很快就会添加它!

编辑

现在您可以通过config/fortify.php 自定义重定向:

'redirects' => [
    'login' => 'dashboard',
    'logout' => 'home',
    'password-confirmation' => null,
    'register' => 'dashboard',
    'email-verification' => null,
    'password-reset' => null,
],

如果您需要对此行为进行高级自定义,请查看docs here

【讨论】:

【解决方案2】:

在这里找到:https://talltips.novate.co.uk/laravel/laravel-8-conditional-login-redirects

  1. 在 app\Http 下创建一个名为 Responses 的文件夹
  2. 创建一个文件 LoginResponse.php
<?php

namespace App\Http\Responses;

use Illuminate\Support\Facades\Auth;
use Laravel\Fortify\Contracts\LoginResponse as LoginResponseContract;

class LoginResponse implements LoginResponseContract
{

    public function toResponse($request)
    {
        
        // below is the existing response
        // replace this with your own code
        // the user can be located with Auth facade
        
        return $request->wantsJson()
                    ? response()->json(['two_factor' => false])
                    : redirect()->intended(config('fortify.home'));
    }

}
  1. 让 Laravel 使用我们新的响应类

这个新类现在取代了之前由 Fortify 注册的 Singleton。

在您的 app\Providers 文件夹中编辑 JetstreamServiceProvider;

在引导方法中,添加对新响应类的引用。当登录完成(并且用户实际上已通过身份验证)时,您的新响应将被调用。

    public function boot()
    {
        $this->configurePermissions();

        Jetstream::deleteUsersUsing(DeleteUser::class);

        // register new LoginResponse
        $this->app->singleton(
            \Laravel\Fortify\Contracts\LoginResponse::class,
            \App\Http\Responses\LoginResponse::class
        );
    }

双重身份验证

如果您将 2FA 与 Jetstream 一起使用,您还需要捕获 TwoFactorLoginResponse。使用相同的方法;

        // register new TwofactorLoginResponse
        $this->app->singleton(
            \Laravel\Fortify\Contracts\TwoFactorLoginResponse::class,
            \App\Http\Responses\LoginResponse::class
        );

如果您希望使用 2FA 登录的用户有不同的行为,您可以返回相同的响应,或者创建额外的响应。

【讨论】:

    猜你喜欢
    • 2021-03-01
    • 2021-06-09
    • 2019-05-06
    • 1970-01-01
    • 2021-03-27
    • 2021-05-16
    • 1970-01-01
    • 1970-01-01
    • 2021-09-18
    相关资源
    最近更新 更多