Skip to content

Open-Source API Development Ecosystem • https://hoppscotch.io • Offline, On-Prem & Cloud • Web, Desktop & CLI • Open-Source Alternative to Postman, Insomnia

Introducing BackendUserInfo

Andrew Bastinf55a995c0a15
diff --git a/helpers/teams/BackendUserInfo.ts b/helpers/teams/BackendUserInfo.ts
new file mode 100644
index 000000000..6e49ec0ae
--- /dev/null
+++ b/helpers/teams/BackendUserInfo.ts
@@ -0,0 +1,86 @@
+import { fb } from "../fb"
+import { BehaviorSubject } from "rxjs"
+import { apolloClient } from "../apollo"
+import gql from "graphql-tag"
+
+/*
+ * This file deals with interfacing data provided by the
+ * Hoppscotch Backend server
+ */
+
+/**
+ * Defines the information provided about a user
+ */
+interface UserInfo {
+ /**
+ * UID of the user
+ */
+ uid: string
+ /**
+ * Displayable name of the user (or null if none available)
+ */
+ displayName: string | null
+ /**
+ * Email of the user (or null if none available)
+ */
+ email: string | null
+ /**
+ * URL to the profile photo of the user (or null if none available)
+ */
+ photoURL: string | null
+ /**
+ * Whether the user has access to Early Access features
+ */
+ eaInvited: boolean
+}
+
+/**
+ * An observable subject onto the currently logged in user info (is null if not logged in)
+ */
+export const currentUserInfo$ = new BehaviorSubject<UserInfo | null>(null)
+
+/**
+ * Initializes the currenUserInfo$ view and sets up its update mechanism
+ */
+export function initUserInfo() {
+ updateUserInfo()
+
+ fb.idToken$.subscribe(token => {
+ if (token) {
+ updateUserInfo()
+ } else {
+ currentUserInfo$.next(null)
+ }
+ })
+}
+
+/**
+ * Runs the actual user info fetching
+ */
+async function updateUserInfo() {
+ try {
+ const { data } = await apolloClient.query({
+ query: gql`
+ query GetUserInfo {
+ me {
+ uid,
+ displayName,
+ email,
+ photoURL,
+ eaInvited
+ }
+ }
+ `
+ })
+
+ currentUserInfo$.next({
+ uid: data.me.uid,
+ displayName: data.me.displayName,
+ email: data.me.email,
+ photoURL : data.me.photoURL,
+ eaInvited: data.me.eaInvited
+ })
+ } catch (e) {
+ currentUserInfo$.next(null)
+ }
+}
diff --git a/layouts/default.vue b/layouts/default.vue
index c15e3c70d..63a0a411a 100644
--- a/layouts/default.vue
+++ b/layouts/default.vue
@@ -14,9 +14,9 @@
</template>
<script>
-import { fb } from "~/helpers/fb"
import { setupLocalPersistence } from "~/newstore/localpersistence"
import { performMigrations } from "~/helpers/migrations"
+import { initUserInfo } from "~/helpers/teams/BackendUserInfo"
export default {
beforeMount() {
@@ -62,6 +62,8 @@ export default {
}
setupLocalPersistence()
+
+ initUserInfo()
},
beforeDestroy() {
document.removeEventListener("keydown", this._keyListener)