Certainly! Here’s a review and some suggestions for your approach to enabling login on a second domain for a single Laravel application.
1. Nginx Configuration
Your Nginx config is mostly correct: both domains point to the same Laravel app directory. This is a common approach for multi-domain apps.
Tip:
In your second domain’s Nginx config, you have:
server_name www.www.domain1.com;
This should probably be:
server_name www.domain2.com;
Otherwise, requests to www.domain2.com won’t match this server block.
2. Session Domain Handling
You’re dynamically setting the session domain in your OriginService. This is a good idea, but there are some caveats:
- Laravel’s session cookie domain is set when the response is created. Changing it at runtime via
config(['session.domain' => ...])works, but it must be set before any session is started or cookies are sent. - Session sharing between domains:
- If you want users to be logged in on both domains with a single login, you must set the session cookie domain to a common parent domain (e.g.,
.domain1.com), and both domains must be subdomains of that parent. - If
domain1.comanddomain2.comare entirely separate, browsers will not share cookies between them for security reasons. - If you only want users to be logged in on the domain they use, your approach is fine.
- If you want users to be logged in on both domains with a single login, you must set the session cookie domain to a common parent domain (e.g.,
3. CSRF Protection
Laravel’s CSRF protection works per domain, as the CSRF token is stored in the session. As long as your session is working per domain, CSRF protection will work as expected.
4. Security
- Validate allowed domains: You’re already checking the host against your
Originmodel. This is good. - HTTPS: Both domains are using HTTPS, which is essential for session security.
- CORS: If you plan to make cross-domain AJAX requests, you’ll need to configure CORS properly.
5. Alternative: Laravel Sanctum
If you ever need true cross-domain authentication (e.g., SPA on one domain, API on another), Laravel Sanctum is the recommended approach. For now, your approach is fine for traditional web apps.
6. Summary
- Your approach is valid for separate logins per domain.
- Session sharing between totally different domains is not possible due to browser security.
- Make sure your Nginx server_name is correct for each domain.
- Set the session domain before the session starts (ideally in a middleware that runs before the session is started).
Example: Middleware for Session Domain
Instead of doing this in AppServiceProvider, consider a middleware:
php artisan make:middleware SetSessionDomain
// app/Http/Middleware/SetSessionDomain.php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use App\Models\Origin;
class SetSessionDomain
{
public function handle(Request $request, Closure $next)
{
if (Origin::where('url', $request->getHost())->exists()) {
config(['session.domain' => $request->getHost()]);
}
return $next($request);
}
}
Register this middleware in app/Http/Kernel.php under web middleware group, before the StartSession middleware.
Conclusion
- Your approach is secure and common for multi-domain apps on the same codebase.
- You cannot share sessions between unrelated domains due to browser restrictions.
- Consider using middleware for setting session domain.
- If you need cross-domain authentication, look into Laravel Sanctum.
Let me know if you have any follow-up questions!