skip to Main Content

I am trying to redirect user to different url after certain event has fired but I keep receiving this error:

SymfonyComponentHttpFoundationResponse::setContent(): Argument #1 ($content) must be of type ?string, IlluminateRoutingRedirector given, called in /var/www/html/vendor/laravel/framework/src/Illuminate/Http/Response.php on line 72

I have event listener in EventServiceProvider:

SlidesSaml2EventsSignedIn::class => [
   SamlLoginListener::class.'@handle',
]

and then in SamlLoginListener:

public function handle(SignedIn $event)
    {
        $messageId = $event->auth->getLastMessageId();
        // your own code preventing reuse of a $messageId to stop replay attacks
        $samlUser = $event->auth->getSaml2User();

        $user_data = [
            'id' => $samlUser->getUserId(),
            'attributes' => $samlUser->getAttributes(),
            'assertion' => $samlUser->getRawSamlAssertion()
        ];

        AuthController::saml_login($user_data);
    }

Then In the AuthController::saml_login I tried all of these but the response was allways the same error:

            return redirect()->away($frontend_url, 302, [
                'X-SSO-AUTH-TOKENS' => json_encode($data),
            ]);

//

            $response = new RedirectResponse($redirect_url, 302, [
                'X-SSO-AUTH-TOKENS' => json_encode($data),
            ]);
            return $response;
//
            
            return redirect($redirect_url, 302, [
                'X-SSO-AUTH-TOKENS' => json_encode($data),
            ]);

I decided to try it again by just returning ‘ok’ but still received the same error. I tried clearing the cache – no result.

Here is the full error trace: https://pastebin.com/4eZYni0w

2

Answers


  1. Chosen as BEST ANSWER

    I ended up doing this and it worked:

    header("Location: " . $redirect_url); 
    header("X-SSO-AUTH-TOKENS: " . json_encode($data)); 
    exit();
    

  2. The answer to the problem is in the error message. Your error is saying that the redirect helper method expects a string, but you’re passing an object.

    Not sure what version of Laravel you’re using but in Laravel, Redirect responses are instances of the IlluminateHttpRedirectResponse class and contain the proper headers needed to redirect the user to another URL.

    you can try:

    return redirect('/frontend_route');

    Redirect to named route:

    return redirect()->route('frontend_route');
    

    If this your route has parameters like in your case, here’s what to do:

    return redirect()->route('frontend_route', ['X-SSO-AUTH-TOKENS' => json_encode($data)]);
    

    Hope this helps.

    source: Redirects in Laravel 8

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search