Sitemap

How to share sessions between two Laravel applications

3 min readAug 23, 2019

Introduction

If you have a large ecosystem with multiple applications, it’s nice to have a common user account which you can use to authenticate in these separate applications. So when a user authenticates any of these apps, they will be logged in everywhere.

What you’ll need to do

  • Share the cookies, sessions and users between the applications
  • Use the same encryption key
  • Use the same domain

So let’s get started.

Create your common database

Here you will only store your common data like your users, sessions, subscriptions etc. Make sure that all your applications can access this database.

Step 1: Update the database configurations

Add a new database connection

You need to do this in all of your applications.

In your config/database.php in the connections array specify your “common_database” connection. Just copy one of your existing configs.

Configure your common models

Edit the models you want to store in the common database and set the connection property.

You can do this in your migration classes too.

Update your validation rules

If you used validation rules to check the users table for example, you have to update them with the connection name.

For example in your RegisterController.

Step 2: Configure sessions

Basically you need the exact same sessions configuration in all of your applications, also the same encryption key.

Store the sessions in the database

This command will create a migration for your sessions:

php artisan session:table

Open up your newly created migration file and set the connection like we did before.

Now in your .env file set the session driver and connection.

SESSION_DRIVER=database
SESSION_CONNECTION=common_database

Set your session cookie domain.

You need to set your domain address in the session configuration, so all of your applications will have access to the cookies. It doesn’t matter where you log in, you will be logged in everywhere.

So add this to your .env file:

SESSION_DOMAIN=".example.com"

(Make sure to include a dot before your domain name.)

Update application keys

Make sure the “APP_KEY” variable in your .env file has the same value in all your applications. Just copy one and paste it everywhere.

That’s it. You are done.

Don’t forget to clear the cookies for all of your apps before you start testing.

You can do that in the console with the Clear site data button:

So now your users and sessions are stored in a common place, if you log in one of you applications, you will be logged in everywhere. If you delete a session from the database, the user will be logged out from that device (browser), so this may add additional cool functionality to your system.

Let me know

I hope this article was easy to follow and it helped you. Let me know if you got stuck and I’ll try to help you.

Follow me on Twitter: @ZsoltGyure

--

--

Zoltie
Zoltie

Written by Zoltie

Software engineer and @laravelphp specialist. Hobby indie game developer. Owner of @skobelwebsites .

Responses (8)