6

在 Laravel fortify on custom authentication process 中,我无法重定向到带有错误消息的登录页面,而我们在 Auth 中可以做到这一点。这是自定义文档链接:https ://jetstream.laravel.com/1.x/features/authentication.html#customizing-the-authentication-process

        if ($user && Hash::check($request->password, $user->password) && $user->status == 'active') {
            return $user;
        } elseif ($user->status == 'inactive') {
            //redirect with some error message to login blade
        } elseif ($user->status == 'blocked') {
            //redirect with some error message to login blade
        }

请帮我解决这个问题。

4

6 回答 6

8

I understand your frustration with the documentation (or lack thereof). I had a similar problem and this is how I managed to do it:

if ($user && in_array($user->status_id, [1,2,3])) {
    if (Hash::check($request->password, $user->password)) {
        return $user;
    }
}
else {
    throw ValidationException::withMessages([
        Fortify::username() => "Username not found or account is inactive. Please check your username.",
    ]);
}

https://github.com/laravel/fortify/issues/94#issuecomment-700777994

于 2020-10-14T07:43:05.927 回答
7

对于那些来自谷歌搜索并使用Laravel Jetstream(使用 Fortify) Snapey的人:Laracasts回答了这个问题并制作了一个教程,使用你自己的LoginResponse来覆盖默认登录行为。

我是这样制作的,但是您当然应该根据自己的需要创建自己的。

// app/Http/Responses/LoginResponse.php
namespace App\Http\Responses;

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

class LoginResponse implements LoginResponseContract
{

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

}

下一步修改它JetstreamServiceProvider以使用您的LoginReponse

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
    );
}

希望它可以节省您的时间。

于 2020-12-01T01:40:19.977 回答
3

对于只需要自定义 JSON 响应而不是默认的{two_factor: false}.

创建一个实现LoginResponseFortify 合同的自定义响应(在这种情况下,我将返回用户对象):

<?php

declare(strict_types=1);

namespace App\Http\Responses;

use Laravel\Fortify\Contracts\LoginResponse as LoginResponseContract;
use Symfony\Component\HttpFoundation\Response;

class LoginResponse implements LoginResponseContract
{
    public function toResponse($request): Response
    {
        return response()->json(auth()->user());
    }
}

将此行添加到以下引导方法FortifyServiceProvider

$this->app->singleton(LoginResponseContract::class, LoginResponse::class);

确保您已正确导入命名空间:

use App\Http\Responses\LoginResponse;
use Laravel\Fortify\Contracts\LoginResponse as LoginResponseContract;

如果您仍然没有从服务器获得 JSON,请确保您的请求具有正确的标头。它应该是一个接受的 XHR 请求application/json

灵感来自 Laravel 新闻的这篇文章。

于 2021-05-02T16:11:10.067 回答
1

在第 66 行转到 \config\fortify.php,将 home 值更改为您想要的任何路径

'家' => '/仪表板',

于 2021-05-23T02:24:34.003 回答
1

例如 - 登录/注销/注册后的自定义重定向。通过请求参数 - 别名

在 FortifyServiceProvider 类中

添加。

use Laravel\Fortify\Contracts\LoginResponse;
use Laravel\Fortify\Contracts\LogoutResponse;
use Laravel\Fortify\Contracts\RegisterResponse;

然后在注册方法中。

    $this->app->instance(LogoutResponse::class, new class implements LogoutResponse {
        public function toResponse($request)
        {
            return redirect('/'.$request->alias);
        }
    });

    $this->app->instance(LoginResponse::class, new class implements LoginResponse {
        public function toResponse($request)
        {
            return redirect()->intended('/'.$request->alias.'/admin/dashboard');
        }
    });

    $this->app->instance(RegisterResponse::class, new class implements RegisterResponse {
        public function toResponse($request)
        {
            return redirect('/'.$request->alias.'/admin/dashboard');
        }
    });
于 2021-06-17T05:53:36.037 回答
0

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

这里有一个开放的问题要求这种行为:https ://github.com/laravel/fortify/issues/77

很有可能很快就会添加!

于 2020-09-26T07:36:41.187 回答