From d5d516ce18fe5f1654d493cf18694ddc0042e11d Mon Sep 17 00:00:00 2001 From: Akash K <57758277+amk-dev@users.noreply.github.com> Date: Wed, 1 Feb 2023 20:47:22 +0530 Subject: [PATCH] chore: abstract auth from hoppscotch/commons to hoppscotch/web (#2899) --- .../src/components/app/DeveloperOptions.vue | 10 +- .../src/components/app/Footer.vue | 7 +- .../src/components/app/Header.vue | 8 +- .../components/collections/ImportExport.vue | 7 +- .../collections/graphql/ImportExport.vue | 7 +- .../src/components/collections/index.vue | 7 +- .../components/environments/ChooseType.vue | 11 +- .../components/environments/ImportExport.vue | 7 +- .../src/components/environments/index.vue | 7 +- .../src/components/firebase/Login.vue | 76 ++- .../src/components/firebase/Logout.vue | 4 +- .../src/components/profile/Shortcodes.vue | 11 +- .../src/components/profile/UserDelete.vue | 4 +- .../src/components/teams/index.vue | 4 +- .../hoppscotch-common/src/composables/auth.ts | 51 +- .../src/helpers/backend/GQLClient.ts | 88 ++-- .../src/helpers/fb/analytics.ts | 6 +- .../hoppscotch-common/src/helpers/fb/auth.ts | 434 ----------------- .../src/helpers/fb/collections.ts | 21 +- .../src/helpers/fb/environments.ts | 43 +- .../src/helpers/fb/history.ts | 41 +- .../hoppscotch-common/src/helpers/fb/index.ts | 4 +- .../src/helpers/fb/request.ts | 7 +- .../src/helpers/fb/settings.ts | 19 +- .../src/helpers/teams/BackendUserInfo.ts | 75 --- .../src/helpers/teams/TeamListAdapter.ts | 6 +- packages/hoppscotch-common/src/index.ts | 26 +- .../hoppscotch-common/src/modules/sentry.ts | 4 +- .../hoppscotch-common/src/pages/enter.vue | 32 +- .../hoppscotch-common/src/pages/join-team.vue | 13 +- .../hoppscotch-common/src/pages/profile.vue | 29 +- .../hoppscotch-common/src/platform/auth.ts | 214 +++++++++ .../hoppscotch-common/src/platform/index.ts | 13 + packages/hoppscotch-common/src/platform/ui.ts | 8 + packages/hoppscotch-web/package.json | 2 + packages/hoppscotch-web/src/firebase/auth.ts | 436 ++++++++++++++++++ packages/hoppscotch-web/src/main.ts | 5 +- pnpm-lock.yaml | 47 +- 38 files changed, 977 insertions(+), 817 deletions(-) delete mode 100644 packages/hoppscotch-common/src/helpers/fb/auth.ts delete mode 100644 packages/hoppscotch-common/src/helpers/teams/BackendUserInfo.ts create mode 100644 packages/hoppscotch-common/src/platform/auth.ts create mode 100644 packages/hoppscotch-common/src/platform/index.ts create mode 100644 packages/hoppscotch-common/src/platform/ui.ts create mode 100644 packages/hoppscotch-web/src/firebase/auth.ts diff --git a/packages/hoppscotch-common/src/components/app/DeveloperOptions.vue b/packages/hoppscotch-common/src/components/app/DeveloperOptions.vue index 02d4f8ea4..f2f63346f 100644 --- a/packages/hoppscotch-common/src/components/app/DeveloperOptions.vue +++ b/packages/hoppscotch-common/src/components/app/DeveloperOptions.vue @@ -29,10 +29,7 @@ import IconCheck from "~icons/lucide/check" import { copyToClipboard } from "~/helpers/utils/clipboard" import { useI18n } from "@composables/i18n" import { useToast } from "@composables/toast" -import { useReadonlyStream } from "@composables/stream" -import { authIdToken$ } from "~/helpers/fb/auth" - -const userAuthToken = useReadonlyStream(authIdToken$, null) +import { platform } from "~/platform" const t = useI18n() @@ -53,8 +50,9 @@ const copyIcon = refAutoReset( // Copy user auth token to clipboard const copyUserAuthToken = () => { - if (userAuthToken.value) { - copyToClipboard(userAuthToken.value) + const token = platform.auth.getDevOptsBackendIDToken() + if (token) { + copyToClipboard(token) copyIcon.value = IconCheck toast.success(`${t("state.copied_to_clipboard")}`) } else { diff --git a/packages/hoppscotch-common/src/components/app/Footer.vue b/packages/hoppscotch-common/src/components/app/Footer.vue index 62ca6ea76..3c02499bb 100644 --- a/packages/hoppscotch-common/src/components/app/Footer.vue +++ b/packages/hoppscotch-common/src/components/app/Footer.vue @@ -219,7 +219,7 @@ import { showChat } from "@modules/crisp" import { useSetting } from "@composables/settings" import { useI18n } from "@composables/i18n" import { useReadonlyStream } from "@composables/stream" -import { currentUser$ } from "~/helpers/fb/auth" +import { platform } from "~/platform" import { TippyComponent } from "vue-tippy" import { getPlatformSpecialKey as getSpecialKey } from "~/helpers/platformutils" import { invokeAction } from "@helpers/actions" @@ -236,7 +236,10 @@ const SIDEBAR_ON_LEFT = useSetting("SIDEBAR_ON_LEFT") const navigatorShare = !!navigator.share -const currentUser = useReadonlyStream(currentUser$, null) +const currentUser = useReadonlyStream( + platform.auth.getCurrentUserStream(), + platform.auth.getCurrentUser() +) watch( () => ZEN_MODE.value, diff --git a/packages/hoppscotch-common/src/components/app/Header.vue b/packages/hoppscotch-common/src/components/app/Header.vue index b89c23bad..f20e619cc 100644 --- a/packages/hoppscotch-common/src/components/app/Header.vue +++ b/packages/hoppscotch-common/src/components/app/Header.vue @@ -171,11 +171,10 @@ import IconUploadCloud from "~icons/lucide/upload-cloud" import IconUserPlus from "~icons/lucide/user-plus" import { breakpointsTailwind, useBreakpoints, useNetwork } from "@vueuse/core" import { pwaDefferedPrompt, installPWA } from "@modules/pwa" -import { probableUser$ } from "@helpers/fb/auth" +import { platform } from "~/platform" import { useI18n } from "@composables/i18n" import { useReadonlyStream } from "@composables/stream" import { invokeAction } from "@helpers/actions" -import { platform } from "~/index" const t = useI18n() @@ -194,7 +193,10 @@ const mdAndLarger = breakpoints.greater("md") const network = reactive(useNetwork()) -const currentUser = useReadonlyStream(probableUser$, null) +const currentUser = useReadonlyStream( + platform.auth.getProbableUserStream(), + platform.auth.getProbableUser() +) // Template refs const tippyActions = ref(null) diff --git a/packages/hoppscotch-common/src/components/collections/ImportExport.vue b/packages/hoppscotch-common/src/components/collections/ImportExport.vue index a46f24e19..f224f7b20 100644 --- a/packages/hoppscotch-common/src/components/collections/ImportExport.vue +++ b/packages/hoppscotch-common/src/components/collections/ImportExport.vue @@ -172,7 +172,7 @@ import { HoppRESTRequest, HoppCollection } from "@hoppscotch/data" import { useI18n } from "@composables/i18n" import { useReadonlyStream } from "@composables/stream" import { useToast } from "@composables/toast" -import { currentUser$ } from "~/helpers/fb/auth" +import { platform } from "~/platform" import { appendRESTCollections, restCollections$ } from "~/newstore/collections" import { RESTCollectionImporters } from "~/helpers/import-export/import/importers" import { StepReturnValue } from "~/helpers/import-export/steps" @@ -263,7 +263,10 @@ watch(inputChooseGistToImportFrom, (url) => { }) const myCollections = useReadonlyStream(restCollections$, []) -const currentUser = useReadonlyStream(currentUser$, null) +const currentUser = useReadonlyStream( + platform.auth.getCurrentUserStream(), + platform.auth.getCurrentUser() +) const importerAction = async (stepResults: StepReturnValue[]) => { if (!importerModule.value) return diff --git a/packages/hoppscotch-common/src/components/collections/graphql/ImportExport.vue b/packages/hoppscotch-common/src/components/collections/graphql/ImportExport.vue index 4a93ee248..9874609cf 100644 --- a/packages/hoppscotch-common/src/components/collections/graphql/ImportExport.vue +++ b/packages/hoppscotch-common/src/components/collections/graphql/ImportExport.vue @@ -99,7 +99,7 @@ import IconFolderPlus from "~icons/lucide/folder-plus" import IconDownload from "~icons/lucide/download" import IconGithub from "~icons/lucide/github" import { computed, ref } from "vue" -import { currentUser$ } from "~/helpers/fb/auth" +import { platform } from "~/platform" import { useI18n } from "@composables/i18n" import { useReadonlyStream } from "@composables/stream" import { useToast } from "@composables/toast" @@ -120,7 +120,10 @@ const emit = defineEmits<{ const toast = useToast() const t = useI18n() const collections = useReadonlyStream(graphqlCollections$, []) -const currentUser = useReadonlyStream(currentUser$, null) +const currentUser = useReadonlyStream( + platform.auth.getCurrentUserStream(), + platform.auth.getCurrentUser() +) // Template refs const tippyActions = ref(null) diff --git a/packages/hoppscotch-common/src/components/collections/index.vue b/packages/hoppscotch-common/src/components/collections/index.vue index b84d75207..5f51c1ce4 100644 --- a/packages/hoppscotch-common/src/components/collections/index.vue +++ b/packages/hoppscotch-common/src/components/collections/index.vue @@ -241,7 +241,7 @@ import { } from "~/helpers/backend/helpers" import { HoppRequestSaveContext } from "~/helpers/types/HoppRequestSaveContext" import * as E from "fp-ts/Either" -import { currentUser$ } from "~/helpers/fb/auth" +import { platform } from "~/platform" import { createCollectionGists } from "~/helpers/gist" import { invokeAction } from "~/helpers/actions" @@ -318,7 +318,10 @@ const confirmModalTitle = ref(null) const filterTexts = ref("") -const currentUser = useReadonlyStream(currentUser$, null) +const currentUser = useReadonlyStream( + platform.auth.getCurrentUserStream(), + platform.auth.getCurrentUser() +) const myCollections = useReadonlyStream(restCollections$, [], "deep") // Export - Import refs diff --git a/packages/hoppscotch-common/src/components/environments/ChooseType.vue b/packages/hoppscotch-common/src/components/environments/ChooseType.vue index 0aae6bfa0..ddfa2d620 100644 --- a/packages/hoppscotch-common/src/components/environments/ChooseType.vue +++ b/packages/hoppscotch-common/src/components/environments/ChooseType.vue @@ -78,7 +78,7 @@ import { nextTick, ref, watch } from "vue" import { GetMyTeamsQuery } from "~/helpers/backend/graphql" import { onLoggedIn } from "@composables/auth" -import { currentUserInfo$ } from "~/helpers/teams/BackendUserInfo" +import { platform } from "~/platform" import TeamListAdapter from "~/helpers/teams/TeamListAdapter" import { useReadonlyStream } from "@composables/stream" import { useLocalState } from "~/newstore/localstate" @@ -111,7 +111,10 @@ const emit = defineEmits<{ (e: "update-selected-team", team: SelectedTeam): void }>() -const currentUser = useReadonlyStream(currentUserInfo$, null) +const currentUser = useReadonlyStream( + platform.auth.getCurrentUserStream(), + platform.auth.getCurrentUser() +) const adapter = new TeamListAdapter(true) const myTeams = useReadonlyStream(adapter.teamList$, null) @@ -138,7 +141,9 @@ watch( ) onLoggedIn(() => { - adapter.initialize() + try { + adapter.initialize() + } catch (e) {} }) const onTeamSelectIntersect = () => { diff --git a/packages/hoppscotch-common/src/components/environments/ImportExport.vue b/packages/hoppscotch-common/src/components/environments/ImportExport.vue index 4f4142cea..842366bca 100644 --- a/packages/hoppscotch-common/src/components/environments/ImportExport.vue +++ b/packages/hoppscotch-common/src/components/environments/ImportExport.vue @@ -107,7 +107,7 @@ import IconDownload from "~icons/lucide/download" import IconGithub from "~icons/lucide/github" import { computed, ref } from "vue" import { Environment } from "@hoppscotch/data" -import { currentUser$ } from "~/helpers/fb/auth" +import { platform } from "~/platform" import axios from "axios" import { useI18n } from "@composables/i18n" import { useReadonlyStream } from "@composables/stream" @@ -141,7 +141,10 @@ const t = useI18n() const loading = ref(false) const myEnvironments = useReadonlyStream(environments$, []) -const currentUser = useReadonlyStream(currentUser$, null) +const currentUser = useReadonlyStream( + platform.auth.getCurrentUserStream(), + platform.auth.getCurrentUser() +) // Template refs const tippyActions = ref(null) diff --git a/packages/hoppscotch-common/src/components/environments/index.vue b/packages/hoppscotch-common/src/components/environments/index.vue index 881e6e130..61c6435bd 100644 --- a/packages/hoppscotch-common/src/components/environments/index.vue +++ b/packages/hoppscotch-common/src/components/environments/index.vue @@ -183,7 +183,7 @@