Skip to content

Commit

Permalink
multi tenant fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
zlayine committed Aug 17, 2023
1 parent 80c529b commit ca88022
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 35 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
13 changes: 10 additions & 3 deletions resources/js/components/pages/Setup.vue
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

<script setup lang="ts">
import { Form } from 'vee-validate';
import { ref } from 'vue';
import { ref, watch } from 'vue';
import { useRouter } from 'vue-router';
import { useAppStore } from '~/store';
import Btn from '~/components/Btn.vue';
Expand Down Expand Up @@ -74,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 @@ -93,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
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 === 'true';
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

0 comments on commit ca88022

Please sign in to comment.