laravel / fortify

Backend controllers and scaffolding for Laravel authentication.
https://laravel.com/docs/fortify
MIT License
1.62k stars 294 forks source link

Fortify can't return Inertia Response #42

Closed amiranagram closed 4 years ago

amiranagram commented 4 years ago

Description:

TypeError
Argument 1 passed to Symfony\Component\HttpFoundation\Response::setContent() must be of the type string or null, object given, called in laravel\framework\src\Illuminate\Http\Response.php on line 65

I did some debugging, and basically when you return an Inertia response in a common controller Illuminate\Routing\Router::toResponse() receives Inertia\Response object where then it calls its own toResponse() to return Illuminate\Http\Response and all is good.

But, using:

Fortify::loginView(function () {
    return inertia('Auth/Login');
});

it fails to build the response because of the SimpleViewResponse and simply returns and Inertia\Response object instead of a string, hence the error above.

Steps To Reproduce:

Configure Inertia, make a Vue component resources/js/Pages/Auth/Login.vue and in FortifyServiceProvider::boot() paste:

Fortify::loginView(function () {
    return inertia('Auth/Login');
});
markbiek commented 4 years ago

I came to leave this exact issue. Thanks @classjunky

amiranagram commented 4 years ago

UPDATE:

Here's a quick fix:

Fortify::loginView(function (Request $request) {
    return inertia('Auth/Login')->toResponse($request);
});

@markbiek

leo95batista commented 4 years ago

I have the same problem, any official solution?

amiranagram commented 4 years ago

@leo95batista The solution I provided above works just fine, you can see that on https://xp.amirrami.com.

Although I wish for a simpler solution, since I, like most Laravel developers like to keep it simple.

driesvints commented 4 years ago

Feel free to pr, thanks.

gianlucasg commented 4 years ago

Could you tell me how to exactly config Inertia in FortifyServiceProvider? This is how i have FortifyServiceProvider img1

P-James commented 4 years ago

This solution is causing
ErrorException strpos() expects parameter 1 to be string, object given

from \vendor\laravel\framework\src\Illuminate\View\ViewName.php:17

for me

Edit: Updating composer dependencies AND using double quotes on "Auth/Login" has fixed it

emlazaro commented 3 years ago

I wonder if this is what you're looking for... maybe this could help you or someone out there :)

Using Inertia alone...

Fortify::loginView(function () {
    return Inertia::render('Auth/Login');
});

OR

Using Ziggy with Inertia...

  Fortify::loginView(function () {
      return route('login');
  });

Given that route('login') is declared as...

//routes/web.php

Route::get('login', [LoginController::class, 'create'])->name('login');
//Auth/LoginController.php

public function create(Request $request, Router $router)
{
    return Inertia::render('Auth/Login');
}