Ashraam's avatar
Level 41

Fortify and wildcard subdomain routing

Hi,

I've got some troubles with Laravel Fortify while using wildcard domain routing.

I am using routing like this {brand}.my-website.test, login and logout work out of the box, I just have to pass the {brand} variable when using the route() helper it's ok.

The troubles started with the reset password, I had to overwrite the response like this in the FortifyServiceProvider

$this->app->singleton(PasswordResetResponse::class, CustomPasswordResetResponse::class);

CustomPasswordResetResponse

class CustomPasswordResetResponse implements Responsable
{
    /**
     * Create an HTTP response that represents the object.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function toResponse($request)
    {
        return redirect()->route('login', ['brand' => $request->brand])->with('passwordReset', "my message");
    }
}

This worked.

But now I've got the same problem with confirm password, I've tried to do the same thing with the appropriate response but it's not working, it's not even passing in the response (I dd'ed inside the vendor folder [never do this at home]),

So I have this question, do I miss something in Fortify the handle the wildcard subdomains ? A config ? Something else ?

Do someone used Fortify with wildcard subdomains and how did you handled those problems ?

Thanks

0 likes
1 reply
LaryAI's avatar
Level 58

When working with Laravel Fortify and wildcard subdomain routing, you need to ensure that all routes and responses are correctly handling the subdomain parameter. You've already managed to customize the password reset response, and now you need to do something similar for the password confirmation process.

Here's a step-by-step guide to address the issue with password confirmation:

  1. Customize the Password Confirmation Response:

    Similar to how you customized the password reset response, you need to create a custom response for password confirmation. Laravel Fortify uses the PasswordConfirmedResponse interface for this purpose.

    First, create a custom response class:

    namespace App\Http\Responses;
    
    use Laravel\Fortify\Contracts\PasswordConfirmedResponse as PasswordConfirmedResponseContract;
    use Illuminate\Http\Request;
    
    class CustomPasswordConfirmedResponse implements PasswordConfirmedResponseContract
    {
        /**
         * Create an HTTP response that represents the object.
         *
         * @param  \Illuminate\Http\Request  $request
         * @return \Symfony\Component\HttpFoundation\Response
         */
        public function toResponse($request)
        {
            return redirect()->route('home', ['brand' => $request->brand])
                             ->with('status', 'Password confirmed successfully.');
        }
    }
    
  2. Register the Custom Response in FortifyServiceProvider:

    In your FortifyServiceProvider, bind your custom response class to the PasswordConfirmedResponse interface.

    use App\Http\Responses\CustomPasswordConfirmedResponse;
    use Laravel\Fortify\Contracts\PasswordConfirmedResponse;
    
    public function register()
    {
        $this->app->singleton(PasswordConfirmedResponse::class, CustomPasswordConfirmedResponse::class);
    }
    
  3. Ensure Routes Handle the Subdomain:

    Make sure that your routes are set up to handle the {brand} subdomain. For example:

    Route::domain('{brand}.my-website.test')->group(function () {
        Route::get('/home', [HomeController::class, 'index'])->name('home');
        // Other routes...
    });
    
  4. Pass the Subdomain Parameter:

    When using the route() helper, ensure that you pass the brand parameter wherever necessary. This is crucial for maintaining the correct subdomain context.

By following these steps, you should be able to handle password confirmation with wildcard subdomains in Laravel Fortify. If you encounter any further issues, double-check that all routes and responses are correctly configured to handle the {brand} parameter.

Please or to participate in this conversation.