Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Bartude's avatar

Same Database Maintain Login Across Multiple Apps

Hey guys,

I've seen various similar discussions here, but couldn't find any that fit my needs.

I have one domain, which is going to have a couple of Laravel 5.4 apps. All of them are connected to the same database, meaning they all share the users table.

I would like to know how to, for example, when a user logs in on one of the apps, if he/she goes to another app on the same domain, they will be automatically logged in.

I think I need to use Sessions, but I'm not really sure where I would have to apply it. Any help?

0 likes
12 replies
J_shelfwood's avatar

I'm not sure it is possible to use the same users table for multiple applications, however I can tell you that most people will refer to Laravel Passport, allowing you to have one central DB for shared information and allow other apps to access it. Like how github can be used to login on some apps.

https://laravel.com/docs/5.4/passport

There's also a great showcase of passport by taylor otwell on the site

1 like
Bartude's avatar

@Hawkleaf Is it necessary to go that far? I mean with tokens and what not.

Isn't the "simplest" way to share the sessions? And then whenever someone tries to access an app, in the middleware(?), I check in the session if somebody is already logged in, and if they are, I automatically log them in and redirect them to where they're going.

And the way I have it, all the apps share the same DB, therefore it's the central DB for everything and all the apps access it.

gustav1105's avatar

I have done something similar on a local machine where to different projects share the same database, and it does work login from any of the projects using the same database, However, the problem is that you now you have duplicated and will when you further your project duplicate a bunch of controllers and models ex.. because the app is powered from it's own laravel framework and it will go through the exact same process just on another application to connect to the database and get the records from there. This is bad practice.

If you are aiming at building multiple applications like this. I would recommend using a API .

Laravel can be your API then you call to that API from the various applications which can also be laravel applications. But you will have to use laravel passport or OAuth at some point otherwise your security will be non existent.

Though it is quite the process if you have not done it before.

But it will solve your problem you can login a user and because that login is not dependent on the application where you are logged in but on the so to speak main application you have flexibility.

Firstly ask yourself do you really need all those different applications for your project if so you have to rethink your workflow.

Borisu's avatar

This shouldn't be a problem as long as you stay on the same domain and you connect to your apps thusly:

domain.com/app1 domain.com/app2 etc.

Then it shouldn't matter.

gustav1105's avatar

@Borisu but would that not be either two different applications each with all its Framework.. Then each app is essentially duplicating everything it shares between each app. So when something that is used the same in both applications needs to change, the change has to be made everywhere. Or how do you mean? Build one app and split it to look like separate apps?

Bartude's avatar

@gustav1105 I would completely agree with you, if the several application were on different domains. But it's like @Borisu said, I'm going to access each application as such.

Main website: domain.com/ First App: domain.com/first Second App: domain.com/second And so on...

I'm doing it like this, because in the future I will probably need to erase or take down temporarily one of the apps, and this way it's much easier.

@gustav1105 Are you still of the same opinion? It's because my knowledge of Laravel isn't that deep, and if I go down the Laravel Passport road, It's going to be harder and take longer than I have planned.

gustav1105's avatar

@Bartude Can you tell me if your apps are all different with some shared things or different instances of the same app? And on one domain does not mean much. You will have to have a server for your applications, the domain is just the part that points to your server.

J_shelfwood's avatar

@Bartude My bad if it isn't the easiest solution. However I am convinced that it is the cleanest solution as in keeping your apps separated.

Passport is indeed harder but very rewarding if successful.

Borisu's avatar

@gustav1105 Yes a single core app. Everything you add to it is just separated by the routes file basically. In the end separate apps are exactly that: the same core with our own add-ons to it. I tend to do that to my smaller apps, if they grow too much I can still extract them.

1 like
Bartude's avatar

@gustav1105 They are all different instances of Laravel with a shared DB.

Basically it's the main website, then I'm going to have several different games, each one with their own instance of Laravel, which I will then access like so:

mainwebsite.com/game1 mainwebsite.com/game2 And so on...

They mainly share the same DB, because it's in the backoffice that I add different elements to all the different games.

gustav1105's avatar

@Bartude, well in this case and only because you do not want to use laravel passport as @Hawkleaf has mentioned. His is still the best long term advice and someday you will have to use laravel passport.But with your current comforatiblity at scaling your app, I think that @Borisa has the best answer for you. Build it as one application and route to the different games. So in essence a single app, that with a solid folder structure to your games, because you can have many controllers and models and everything you want in one application, while still having a seperation of concerns. While still in development mode, If you are not yet comfortable with having all that logic in one place, do it as you are doing it, it will work connecting your single database to all the different projects, but remember that each project works on its own, the only common factor is the database none of its logic. Each game will have its own logic. You will have to rewrite a lot of the same stuff for each one, that is the problem! But you will learn how and what to build for each game.

My advice is to start with the games, each as a project so you can mess up and change things to get it working to your requirements, then when you have those in place.Start working on a single application that has the login and Home and all the bells and whisles, then you can start by implementing those games you already have the code for to your application.

In the process you will learn a lot about what works and what doesn't and the fallout is minimum because you are isolating the projects.

1 like

Please or to participate in this conversation.