When a user logs in to one application, they should also be authenticated in other application without logging in again.
1. Use a Common Authentication System
Ensure both Laravel apps share the same user database (either directly or via API).
2. Use tymon/jwt-auth
in both apps
Install JWT package in both:
Do this in both apps. Say App A and App B
3. Generate Token in App A on Login
In App A, modify the login logic to generate a JWT token:
4. Share JWT Token with App B
When user logs in:
-
Save token in a secure cookie:
✅ This cookie is available to all subdomains, like App A and App B
-
Alternatively, you can redirect to App B with token in query:
5. Accept and Authenticate in App B
In AppB, create a route like:
You can auto-trigger this on page load, or have a middleware that checks and redirects accordingly.
6. Keep It Secure
-
Use HTTPS.
-
Mark cookie as
Secure
,HttpOnly
, andSameSite=Strict
if possible. -
Tokens should expire, and refresh tokens can be used optionally.
-
If hosting under different domains (not subdomains), cookies won't be shareable — you'll need to redirect with the token.
Optional: Middleware in App B
Create a middleware to auto-login using the JWT cookie if user not logged in:
Summary
Feature | Implementation |
---|---|
Token Generation | On login in App A |
Token Sharing | Secure Cookie or URL param |
Token Reading | App B reads token from cookie/URL |
Login Session | Auth::login($user) in App B |
Security | HTTPS, HttpOnly, SameSite, Token Expiry |
No comments:
Post a Comment