Great, Rajesh. Since you’re using Laravel for all microservices (doctors, hospitals, patient portal), I’ll guide you through a complete step-by-step setup to create a centralized auth service at auth.myhospitalnow.com
and integrate it with all other services.
✅ GOAL
Set up:
auth.myhospitalnow.com
→ Central login system (Auth Service)
www.myhospitalnow.com/hospitals
, /doctors
, /patient-portal
→ Use token from Auth to authenticate
🧩 TECH STACK OVERVIEW
- Laravel 11 (or latest)
- Laravel Sanctum for token-based auth (simple, secure for SPAs + microservices)
- All services deployed under same root domain (
.myhospitalnow.com
)
- All Laravel apps share common user/auth table via API
✅ STEP-BY-STEP GUIDE TO SETUP auth.myhospitalnow.com
🔹 1. Create the Auth Service (Laravel App)
composer create-project laravel/laravel auth-service
cd auth-service
🔹 2. Set Sanctum for API Authentication
composer require laravel/sanctum
php artisan vendor:publish --provider="Laravel\Sanctum\SanctumServiceProvider"
php artisan migrate
In config/sanctum.php
, set:
'stateful' => explode(',', env('SANCTUM_STATEFUL_DOMAINS', '.myhospitalnow.com')),
🔹 3. Setup CORS to Allow All Services
Edit config/cors.php
:
'paths' => ['api/*', 'login', 'logout', 'sanctum/csrf-cookie'],
'allowed_origins' => ['https://*.myhospitalnow.com'],
'allowed_headers' => ['*'],
'allowed_methods' => ['*'],
'supports_credentials' => true,
🔹 4. Configure Cookie Domain (Important!)
In config/session.php
:
'domain' => '.myhospitalnow.com',
Also in .env
:
SESSION_DOMAIN=.myhospitalnow.com
SANCTUM_STATEFUL_DOMAINS=.myhospitalnow.com
🔹 5. Create Login, Logout & User APIs
In routes/api.php
:
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Models\User;
Route::post('/login', function (Request $request) {
$credentials = $request->only('email', 'password');
if (!Auth::attempt($credentials)) {
return response()->json(['message' => 'Invalid credentials'], 401);
}
$request->session()->regenerate();
return response()->json(Auth::user());
});
Route::get('/user', function (Request $request) {
return $request->user();
})->middleware('auth:sanctum');
Route::post('/logout', function (Request $request) {
Auth::guard('web')->logout();
$request->session()->invalidate();
$request->session()->regenerateToken();
return response()->json(['message' => 'Logged out']);
});
🔹 6. Expose Auth API to Other Services
Use HTTPS (important for cookies)
Accept cross-origin credentials (via Sanctum + CORS)
Each service (doctor, hospital, patient) will:
🔄 STEP 7: Update Other Laravel Services (Hospital/Doctor/Patient Portals)
In each other service (hospitals
, doctors
, patient-portal
):
📦 A. Install Sanctum (same way as above)
composer require laravel/sanctum
Update .env
:
SANCTUM_STATEFUL_DOMAINS=.myhospitalnow.com
SESSION_DOMAIN=.myhospitalnow.com
⚙️ B. Set CORS and session domain (same as above)
🔌 C. Instead of local login, call Auth API
In your services, do not use native Laravel login — instead, send a POST request to:
POST https://auth.myhospitalnow.com/api/login
with:
{
"email": "user@example.com",
"password": "password"
}
If successful:
- A session cookie will be set for
.myhospitalnow.com
- Now you can use
auth.myhospitalnow.com/api/user
to get current user
🧪 D. On All Pages, Call Auth API to Get User Info
Example (in hospital portal):
$response = Http::withCookies($_COOKIE, '.myhospitalnow.com')
->get('https://auth.myhospitalnow.com/api/user');
$user = $response->json();
Use this to check who’s logged in (doctor, hospital admin, etc.)
✅ BONUS: Add Roles to User Table
In your users
table (central auth DB), add a role
column:
$table->enum('role', ['patient', 'doctor', 'hospital']);
Use this to redirect after login:
role == doctor
→ go to /doctors/dashboard
role == hospital
→ /hospitals/dashboard
- etc.
✅ Architecture Recap
+----------------------------+
| auth.myhospitalnow.com |
| (Laravel + Sanctum) |
+------------+--------------+
|
| Session Cookie (Domain=.myhospitalnow.com)
v
+---------------------+ +---------------------+ +----------------------+
| patient-portal | | hospitals portal | | doctors portal |
| Laravel app | | Laravel app | | Laravel app |
| auth via API | | calls /user on auth | | calls /user on auth |
+---------------------+ +---------------------+ +----------------------+
✅ Final Testing Checklist
| Task | Status |
| ----------------------------------------------- | ------ |
| Sanctum installed on all services | ✅ |
| Session domain set to .myhospitalnow.com
| ✅ |
| Login only via auth.myhospitalnow.com
| ✅ |
| Other services verify session using /api/user
| ✅ |
| Roles handled for redirection | ✅ |
Would you like me to:
- Provide Laravel login controller code calling external auth?
- Give a working Vue/Flutter login flow with token support?
- Set up multi-guard Laravel logic for different roles?
Let me know, and I’ll guide you through the rest!