1

I'm getting a bad URL generated by sendResetLinkResponse controller in laravel 5.8 having the domain twice.

https://api.domain.org/domain.org/password/reset/....

But it should be

https://api.domain.org/password/reset/....

The APP_URL is set to

APP_URL=domain.org

I use a custom config to be able to have as endpoint api.domain.org instead of www.domain.org/api

My configuration is:

protected function mapApiRoutes() {

 Route::domain('api.' .  env('APP_URL'))
   ->middleware('api')
   ->namespace($this->namespace)
   ->group(base_path('routes/api.php'));
}

How can I fix it?

Uncoke
  • 1,832
  • 4
  • 26
  • 58

1 Answers1

1

I would suggest setting up your subdomain to be more dynamic i.e.

Route::domain('api.{domain}')
    ->middleware(['api', function ($request, $next) {
        $request->route()->forgetParameter('domain');

        return $next($request);
    }])
    ->namespace($this->namespace)
    ->group(base_path('routes/api.php'));

The above basically allows for any domain name and then the middleware just removes it from your route params so that it doesn't mess with your route closures or controller methods.

You will also need to add the following to the boot method of your service provider:

Route::pattern('domain', '[a-z0-9.]+');

This way you can use the APP_URL to just be the domain for the site.

Rwd
  • 34,180
  • 6
  • 64
  • 78
  • Thanks Ross! This works! Now I use the full url in .ENV and everything is fine, also the password link! Wow! Thanks! Can you explain to me the patch to the boot? – Uncoke Jul 17 '19 at 11:55
  • Sorry, Ross. I've done a strategic error: I separated the API l logic from the frontend having 2 different servers and domains: www for frontend and API for the backend. So the link to recover password should point to www domain ( I could create a redirect on API side but I prefer to have a clean workflow). Then the frontend will pass the token in background to the API to verify and update. How can update your patch to serve the www url in recover link? Any suggestion? – Uncoke Jul 17 '19 at 13:56
  • 1
    @Uncoke Sorry, I'm not sure what you mean by "patch to the boot"? The `APP_URL` is used when Laravel can't actually figure out what the domain is -- have a look at: https://stackoverflow.com/questions/31129092/what-is-the-significance-of-application-url-in-laravel-5. So if you don't have any jobs, email etc. that require the url to point to the `api` I would just have it point to the `www`. If you do, then you will have to override any of the built in notification yourself. – Rwd Jul 17 '19 at 14:52
  • 1
    @Uncoke https://laravel.com/docs/master/passwords#password-customization and https://pineco.de/customizing-the-password-reset-email-in-laravel/ should point you in the right direction – Rwd Jul 17 '19 at 14:53