Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

"Wildcard" Subdomain for Every Tenant

Premium
4:51

Now, let's talk about subdomains. This is a typical way to structure multi-tenancy projects, and how to separate tenants so every tenant would have a "personal space". Fake, but personal space.

What do I mean by fake? Why is it fake?

Because subdomains can't act as a security filter, it wouldn't be secure enough. For example, we have a tenancy.test domain. And for every tenant/company/team, we create a subdomain like povilas.tenancy.test.

If I take just the URL and filter the data by that URL, anyone could fake my URL without even being logged in. Another way would be to log into another tenant, replace their URL, and land onto my workspace, which is a security issue.

So, in this lesson, we will create subdomains. I will show you how it works, but you need to understand correctly that it is more like the cherry on top, like part of the design of your application, but not the secure authentication of the tenant.

Let's implement these features:

  1. Register the subdomain during the registration and then redirect to the correct subdomain.
  2. When switching between the tenants, redirect them to their subdomain.

Registering the Subdomain

In the registration form, let's add the input for the subdomain.

resources/views/auth/register.blade.php:

// ...
 
<!-- Email Address -->
<div class="mt-4">
<x-input-label for="email" :value="__('Email')" />
<x-text-input id="email" class="block mt-1 w-full" type="email" name="email" :value="old('email')" required autocomplete="username" />
<x-input-error :messages="$errors->get('email')" class="mt-2" />
</div>
 
<!-- Subdomain -->
<div class="mt-4">
<x-input-label for="subdomain" :value="__('Subdomain')" />
<x-text-input id="subdomain" class="block mt-1 mr-2 w-full" type="text" name="subdomain" :value="old('subdomain')" required />
<x-input-error :messages="$errors->get('subdomain')" class="mt-2" />
</div>
 
<!-- Password -->
 
// ...
</x-guest-layout>

Next, create a subdomain column in...

The Full Lesson is Only for Premium Members

Want to access all of our courses? (29 h 14 min)

You also get:

54 courses
Premium tutorials
Access to repositories
Private Discord
Get Premium for $129/year or $29/month

Already a member? Login here

Comments & Discussion

I
inspigo ✓ Link copied!

Where do you check the subdomain assigned to the tenant? Regardless of which subdomain I enter, the dashboard shows me (checking permissions by subdomain does not work).

M
Modestas ✓ Link copied!

You can add the check based on the $user->current_tenant_id in your system. This can be done via middleware or via if conditions where you need it to.

An example is at the end of the video with the TenantController

I
inspigo ✓ Link copied!

Can you give me some code example? I don't know how to apply this to the example in question.

M
Modestas ✓ Link copied!

Example code for what exactly? There is both video and a github repository with the code shown there

I
inspigo ✓ Link copied!

I need code to check whether the subdomain entered by the user is assigned to him. Currently, I enter any subdomain and see my dashboard, e.g. test.mydomain.com, test2.mydomain.com (test2 is not my subdomain tenant).

M
Modestas ✓ Link copied!

Oh, now I understand.

For this, you need to create a custom middleware, which would check if the domain belongs to user.

You can get the subdomain with this code: $request->route('subdomain') and simply compare to auth()->user() models allowed subdomains.


There is no ready-made example of this, sorry!

EA
Eser Alpkaya ✓ Link copied!

what is the name plugin to fill form fastly ?

M
Modestas ✓ Link copied!

The plugin is called FakeFiller in chrome

MA
Muhsin Ahadi ✓ Link copied!

It does not work with Laragon and Apache. What is webserver that you use?

M
Modestas ✓ Link copied!

For Laragon/Apache combination, you have to modify windows HOST file and create EACH tenant as an entry there (domain inside HOST file). It does not automatically resolve it correctly.

We are using Laravel Valet, but I assume you are on windows - so you should probably check out Herd!

NC
Nikola Cava ✓ Link copied!

When using the redirect from the registration (from domain to subdomain), I get the CORS policy error. I set the SESSION_DOMAIN, also the tried adding "." in front of the session domain in config. I am using Herd and the laravel 12 vue starter kit. Any idea if there is any way to solve this without changing the CORS policy?

C
Coffeetime ✓ Link copied!

Having the same issue. Did you get this resolved?

ER
Enrique Robledo ✓ Link copied!

The server side configuration would be great (nginx)