Passing custom data into login.blade.php with Laravel 8 (and Jetstream)

Viewed 2543

Short of implementing my own login controller, is there a way to pass data into views/auth/login.blade.php with Laravel 8 and Jetstream (Livewire)?

In Laravel 7, the LoginController had a showLoginForm() method that you could override and just return a view with additional data. It doesn't seem to be as straightforward with Jetstream.

2 Answers

This is how I solved it: (thru laravel's pipeline's concept)

public function showLoginForm()
    {
        return view('admin.auth.login');
    }

    public function logout()
    {
        Session::flush();
        Auth::logout();
        return redirect('admin/login');
    }

    public function login(Request $request)
    {
        return $this->loginPipeline($request)->then(function ($request) {
            return app(LoginResponse::class);
        });
    }

    protected function loginPipeline($request)
    {
        if (Fortify::$authenticateThroughCallback) {
            return (new Pipeline(app()))->send($request)->through(array_filter(
                call_user_func(Fortify::$authenticateThroughCallback, $request)
            ));
        }

        if (is_array(config('fortify.pipelines.login'))) {
            return (new Pipeline(app()))->send($request)->through(array_filter(
                config('fortify.pipelines.login')
            ));
        }

        return (new Pipeline(app()))->send($request)->through(array_filter([
            config('fortify.limiters.login') ? null : EnsureLoginIsNotThrottled::class,
            RedirectIfTwoFactorAuthenticatable::class,
            AttemptToAuthenticate::class,
            PrepareAuthenticatedSession::class,
        ]));
    }

If you want to do a customize field, lets say "status" => "active". Then you need to copy the RedirectIfTwoFactorAuthenticatable to actions folder. And do something like this:

 return tap($model::where(Fortify::username(), $request->{Fortify::username()})
            ->where('status', 'active')
            ->first(), 
            function ($user) use ($request) {
            if (!$user || !Hash::check($request->password, $user->password)) {
                $this->throwFailedAuthenticationException($request);
            }
        });

Enjoy :)

Related