Skip to content

Commit

Permalink
fix multi tenancy (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
zlayine authored Aug 17, 2023
1 parent d0246b8 commit defea59
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 44 deletions.
6 changes: 5 additions & 1 deletion resources/js/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,14 @@ export class ApiService {
const appStore = useAppStore();

return new Promise((resolve, reject) => {
if (!appStore.config.url) {
reject({ field: 'Error', message: 'No URL provided' });
}

ApiService.request({
url: `${appStore.config.url}graphql${schema}`,
data,
credentials: useAppStore().isMultiTenant ? 'include' : 'omit',
credentials: appStore.isMultiTenant ? 'include' : 'omit',
}).then((res: any) => {
if (res.errors) {
const message = res.errors[0].message;
Expand Down
18 changes: 12 additions & 6 deletions resources/js/components/pages/Setup.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
input-class="block w-full rounded-md border-0 py-1.5 text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:ring-2 focus:ring-inset focus:ring-primary sm:text-sm sm:leading-6"
/>
<FormInput
v-if="!(appStore.config.authorization_token.length > 0)"
v-if="!appStore.isMultiTenant"
v-model="authorizationToken"
label="Authorization Token"
name="authorization"
Expand All @@ -31,8 +31,7 @@

<script setup lang="ts">
import { Form } from 'vee-validate';
import { ref } from 'vue';
import type { Ref } from 'vue';
import { ref, watch } from 'vue';
import { useRouter } from 'vue-router';
import { useAppStore } from '~/store';
import Btn from '~/components/Btn.vue';
Expand All @@ -45,7 +44,7 @@ const router = useRouter();
const appStore = useAppStore();
const isLoading = ref(false);
const url: Ref<URL | undefined> = ref();
const url = ref();
const authorizationToken = ref('');
const formRef = ref();
Expand Down Expand Up @@ -75,7 +74,7 @@ const setupAccount = async () => {
throw new Error('You must use an https hostname');
}
const parsedUrl = new URL(url.value!);
const parsedUrl = new URL(url.value);
if (!(await appStore.checkURL(parsedUrl))) return;
if (
Expand All @@ -94,7 +93,14 @@ const setupAccount = async () => {
};
(async () => {
if (appStore.config.url && appStore.config.authorization_token) redirectToCollections();
if (appStore.hasValidConfig) redirectToCollections();
url.value = appStore.config.url as URL;
})();
watch(
() => appStore.hasValidConfig,
() => {
if (appStore.hasValidConfig) router.push({ name: 'platform.collections' });
}
);
</script>
9 changes: 5 additions & 4 deletions resources/js/components/pages/auth/Login.vue
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,10 @@ const login = async () => {
isLoading.value = false;
return;
}
snackbar.success({ title: 'Logged in successfully', save: false });
await appStore.init();
redirectToCollections();
if (await appStore.init()) {
snackbar.success({ title: 'Logged in successfully', save: false });
redirectToCollections();
}
} catch (e) {
if (snackbarErrors(e)) return;
snackbar.error({
Expand All @@ -118,7 +119,7 @@ const checkVerified = () => {
(async () => {
checkVerified();
if (appStore.loggedIn) {
if (appStore.hasValidConfig) {
redirectToCollections();
}
})();
Expand Down
2 changes: 1 addition & 1 deletion resources/js/components/pages/auth/Register.vue
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ const register = async () => {
};
(async () => {
if (appStore.loggedIn) {
if (appStore.hasValidConfig) {
redirectToLogin();
}
})();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ const requestReset = async () => {
};
(async () => {
if (appStore.loggedIn) {
if (appStore.hasValidConfig) {
redirectToCollections();
}
})();
Expand Down
2 changes: 1 addition & 1 deletion resources/js/components/pages/auth/ResetPassword.vue
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ const resetPassword = async () => {
(async () => {
email.value = route.query.email as string;
if (appStore.loggedIn) {
if (appStore.hasValidConfig) {
redirectToCollections();
}
})();
Expand Down
2 changes: 1 addition & 1 deletion resources/js/config.json.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
"authorization_token": "AUTHORIZATION_TOKEN_VALUE",
"route": "ROUTE_VALUE",
"tenant": "MULTI_TENANCY_VALUE",
"websocket": "WEBSOCKET_URL_VALUE",
"websocket": "WEBSOCKET_VALUE",
"channel": "WEBSOCKET_CHANNEL_VALUE"
}
45 changes: 27 additions & 18 deletions resources/js/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,7 @@ const RPC_URLS = {
};

const parseConfigURL = (url: string): URL => {
try {
return new URL(url);
} catch {
return new URL('https://' + url);
}
return new URL(url);
};

const updateTransaction = async ({
Expand Down Expand Up @@ -115,7 +111,6 @@ export const useAppStore = defineStore('app', {
this.setConfig();
if (!this.config.url) return false;

if (this.isMultiTenant && this.loggedIn) await this.getUser();
const urlConfig = await this.checkURL(this.config.url);
this.config.network = urlConfig.network;
this.config.packages = Object.entries(urlConfig.packages).map(([key, value]: any[]) => {
Expand All @@ -131,38 +126,52 @@ export const useAppStore = defineStore('app', {
link,
};
});

if (this.hasBeamPackage) this.addBeamNavigation();
if (this.hasFuelTanksPackage) this.addFuelTanksNavigation();
if (this.hasMarketplacePackage) this.addMarketplaceNavigation();

this.loggedIn = true;
if (this.isMultiTenant && this.loggedIn) await this.getUser();

return await this.fetchCollectionIds();
} catch (error: any) {
snackbar.error({ title: error });
this.clearLogin();
}

return false;
},
async setupAccount({ url, authorization_token }: { url: URL; authorization_token: string }) {
this.url = url;
this.authorization_token = authorization_token;
this.config.authorization_token = authorization_token;
this.loggedIn = true;

return await this.init();
},
setConfig() {
if (appConfig?.url) this.config.url = parseConfigURL(appConfig.url);
else if (window?.bootstrap?.hostname) this.config.url = parseConfigURL(window.location.origin);
else this.config.url = this.url;
if (appConfig?.tenant) {
this.config.tenant = appConfig.tenant === 'true';
}

if (appConfig?.authorization_token?.length) this.config.authorization_token = appConfig.authorization_token;
else this.config.authorization_token = this.authorization_token;
if (appConfig?.url) {
this.config.url = parseConfigURL(appConfig.url);
} else if (window?.bootstrap?.hostname) {
this.config.url = parseConfigURL(window.location.origin);
} else {
this.config.url = this.url;
}

if (appConfig?.tenant) this.config.tenant = appConfig.tenant;
if (!this.config.tenant && appConfig?.authorization_token?.length) {
this.config.authorization_token = appConfig.authorization_token;
} else if (!this.config.tenant) {
this.config.authorization_token = this.authorization_token;
}

if (appConfig.websocket.length) this.config.webSocket = appConfig.websocket;
if (appConfig.channel.length) this.config.channel = appConfig.channel;
if (appConfig.websocket.length) {
this.config.webSocket = appConfig.websocket;
}
if (appConfig.channel.length) {
this.config.channel = appConfig.channel;
}
},
async checkURL(url: URL) {
try {
Expand Down Expand Up @@ -431,7 +440,7 @@ export const useAppStore = defineStore('app', {
return state.loggedIn && state.user?.apiTokens?.length > 0 && state.user?.account;
}

return state.loggedIn && state.config.url && state.config.authorization_token.length > 0;
return state.loggedIn && state.config.url;
},
isMultiTenant(state: AppState) {
return state.config.tenant;
Expand Down
11 changes: 5 additions & 6 deletions resources/js/util/auth.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export function initAuthGuard(router: Router) {

const validConfig = appStore.hasValidConfig;
const isMultiTenant = appStore.isMultiTenant;
const isLoggedIn = appStore.loggedIn;

const requiresAuth = to.matched.some((record) => record.meta.requiresAuth);
const requiresToken = to.matched.some((record) => record.meta.requiresToken);
Expand All @@ -21,21 +20,21 @@ export function initAuthGuard(router: Router) {
return;
}

if (requiresAuth && !isLoggedIn) {
if (requiresAuth && !validConfig) {
next({ name: 'platform.auth.login' });
} else if (requiresToken && appStore.user && !validConfig) {
next({ name: 'platform.user.settings' });
} else if (to.name == 'platform.auth.login' && isLoggedIn) {
} else if (to.name == 'platform.auth.login' && validConfig) {
next({ name: 'platform.collections' });
} else {
next();
}
} else {
if (requiresAuth && (!validConfig || !isLoggedIn)) {
if (requiresAuth && !validConfig) {
next({ name: 'platform.setup' });
} else if (to.name == 'platform.auth.login' && !isLoggedIn) {
} else if (to.name == 'platform.auth.login' && !validConfig) {
next({ name: 'platform.setup' });
} else if (to.name == 'platform.setup' && validConfig && isLoggedIn) {
} else if (to.name == 'platform.setup' && validConfig) {
next({ name: 'platform.collections' });
} else {
next();
Expand Down
12 changes: 7 additions & 5 deletions src/Console/InstallPlatformUi.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,19 @@ public function handle()
{
$this->resetJSON();

$tenant = $this->askForInput('Do you want to enable multi-tenancy? (yes/no)', 'MULTI_TENANCY_VALUE', false, 'tenant');
$tenant = $this->askForInput('Do you want to enable multi-tenancy? (yes/no)', 'MULTI_TENANCY_VALUE', 'false', 'tenant');

$this->askForInput('Please enter your Enjin Platform URL', 'URL_VALUE', '', 'host');

if ($tenant !=='true') {
$this->askForInput('Please enter the authorization token', 'AUTHORIZATION_TOKEN_VALUE', '', 'token');
} else {
$this->updateConfig('AUTHORIZATION_TOKEN_VALUE', '');
}

$route = $this->askForInput('Please enter the default route path', 'ROUTE_VALUE', '', 'route');
$this->updateRoute($route);
$this->askForInput('Please enter WebSocket URL? (optional)', 'WEBSOCKET_URL_VALUE', '', 'wsurl');
$this->askForInput('Please enter WebSocket URL? (optional)', 'WEBSOCKET_VALUE', '', 'wsurl');
$this->askForInput('Please enter WebSocket channel? (optional)', 'WEBSOCKET_CHANNEL_VALUE', '', 'wschannel');

$this->comment('Installing Platform UI dependencies...');
Expand Down Expand Up @@ -58,7 +60,7 @@ private function askForInput($label, $key, $default = null, $option = null)
{
if (!is_null($optionValue = $this->option($option))) {
if ($option === 'tenant') {
$optionValue = Str::lower($optionValue) === 'yes';
$optionValue = Str::lower($optionValue) === 'yes' ? 'true' : 'false';
}
$this->updateConfig($key, $optionValue);

Expand All @@ -79,7 +81,7 @@ private function askForInput($label, $key, $default = null, $option = null)
}

if ($option === 'tenant') {
$value = Str::lower($value) === 'yes';
$value = Str::lower($value) === 'yes' ? 'true' : 'false';
}

$this->updateConfig($key, $value);
Expand All @@ -95,7 +97,7 @@ private function updateConfig($key, $value)
file_put_contents(
$this->BASE_DIR . 'resources/js/config.json',
str_replace(
is_bool($value) ? "$key" : $key,
$key,
$value,
$appConfig
)
Expand Down

0 comments on commit defea59

Please sign in to comment.