Now, I'm able to login via both domains, which isn't good, I only want the application routes in Spark to be accessible via the app.* subdomain, but I can access them via the frontend primary domain.
This is causing issues with Intercom integration and pinging their secure server because I'm able to login via https://app.example.com/login (can log the Spark user in console), but visit https://example.com/login (no user available) and login again there. Which is obviously odd.
The middleware is handled server-side so there should not be any trouble with Token Mismatch errors due to this - at the end you can do it as a simple redirect.
Check the default middleware RedirectIfAuthenticated on how it generally works.
And then do somehting along the lines of this:
public function handle($request, Closure $next, $guard = null)
{
if($request->route()->getPath() == 'whatever/you/want/to/block') // you can buld this as an in_array() setup if you have many routes to apply it to
{
return redirect('http://subdomain.yourdomain.com/correct/login/url'); // or where every you want to redirect the user to if he request a url that should not work
}
}
return $next($request);
You can also get other parameters off $request if ->route()->getPath() is not what you need.
I use this setup and it works without any trouble.
Thanks, @EventFellows , seems a little messy hard coding the base URL in the redirect, especially when considering you'd have to perform an environment check to point to the correct URI.
But, if it works, then it's a temporary solution. Just seems like there should be a better way than hardcoding redirects for every single Spark route that's exposed to the frontend user.
You are right, but you can always compile the redicret dynamically based on the request, so you do not need to hardcode any domain.
If it is only very few URIs that you want to block it feels ok to me.