5

I have two Laravel applications that use the same database and, therefore, have the same users and passwords.

Let's say the applications are called A and B.

If a user logs into A, what can I do so that they're automatically logged into B? So if they log into A, then when they go to B, they will not have to type in their login information again.

Both applications are on the same domain, but B is a subdomain and is not part of the same project as A.

Thank you in advance!

5
  • couldn't you set a token in the backend, and just have the request include that token and log them in through Auth::login($user); or something similar? Commented Nov 4, 2016 at 1:01
  • I suppose I could do that. How would application B know to check for that when the user goes there from A? Commented Nov 4, 2016 at 1:21
  • 2
    maybe modify the Auth middleware to look for it? or perhaps in the constructor for the controller(s) you just include the Request $request and see if it contains the token. Commented Nov 4, 2016 at 1:27
  • I'll try that, and I'll report back the result. Commented Nov 4, 2016 at 1:29
  • I once did this in php by changing the session cookies path to *.mydomain.foo. Perhaps something like this: stackoverflow.com/questions/34488477/… Commented Mar 20, 2017 at 7:19

1 Answer 1

1

When you say Both applications are on the same domain, but B is a subdomain and is not part of the same project as A. can i safely assume that they are two separate installations of Laravel, but they share the same database? I that's the case, you could try this:

  • Switch the session driver to database in your .env file to "database", for reference you should look at the comments in config/session.php, near the 'driver' => env('SESSION_DRIVER', 'file'), option. Be sure to manage the tables accordingly (see https://laravel.com/docs/5.5/session#driver-prerequisites)
  • In your .env file, specify this row: SESSION_DOMAIN=.domain.ext (notice the dot near the equal sign).

Alternatively, if even the databases are separate you could try the same config as above using the cookie driver instead fo the database. let me know if this helped.

edit: more a suggestion than an answer, laravel is capable of handling multiple websites/domains natively (example: Route::domain('sub.domain.ext'). Pratically speaking, you organize the routes this way in routes/web.php. With a wise database management (and queries) you can easily mantain 2 separate websites within 1 Laravel application. This assuming you have control over your webserver, of course. If that's the case, i would suggest a refractoring. It's really worth the effort.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.