NuxtJS Subdomain

Viewed 1239

I'm completely new to nuxtjs and I want my router to work with a subdomain, thinking maybe with some middleware for redirecting the user if database/subdmoain doesn't exist or if their auth is no longer valid.

I'm setting up a multi-tenant database so each tenant has there own unique subdomain, using the subdomain that will tell my api backend what database to be connecting too.

I want my default database to use app.site.com and then tenants will have their own database via company1.site.com company2.site.com

1 Answers

You can check your subdomains in the middleware. Then fill your store accordingly so that the correct database is queried.

export default function ({ store }) {

    const host = window.location.host;
    const subdomain = host.split('.')[0];

    switch (subdomain) {
        case 'company1.site.com':
            store.commit('setDatabase', payload)
            break;

        case 'company2.site.com':
            store.commit('setDatabase', payload)
            break;

        default:
            store.commit('setDatabase', payload)
            break;
    }

}
Related