-
Notifications
You must be signed in to change notification settings - Fork 937
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use only a single account in VSCode. Support IDX Auth (#7420)
* Auth working in IDX? * Remove concept of multiple accounts in vscode. Support IDX Auth * add changelog. small cleanup * Remove console.log * remove comment * revert project changes * address comments * add loading spinner * remove spacer * formatting --------- Co-authored-by: Joe Hanley <joehanley@google.com>
- Loading branch information
Showing
13 changed files
with
172 additions
and
304 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,85 +1,62 @@ | ||
import { computed, effect } from "@preact/signals-react"; | ||
import { Signal, computed, effect } from "@preact/signals-react"; | ||
import { Disposable, TelemetryLogger } from "vscode"; | ||
import { ServiceAccountUser } from "../types"; | ||
import { User as AuthUser } from "../../../src/types/auth"; | ||
import { ExtensionBrokerImpl } from "../extension-broker"; | ||
import { getAccounts, login, logoutUser } from "../cli"; | ||
import { login, logoutUser, requireAuthWrapper } from "../cli"; | ||
import { globalSignal } from "../utils/globals"; | ||
import { DATA_CONNECT_EVENT_NAME } from "../analytics"; | ||
|
||
type User = ServiceAccountUser | AuthUser; | ||
|
||
/** Available user accounts */ | ||
export const users = globalSignal<Record<string /** email */, User>>({}); | ||
|
||
/** Currently selected user email */ | ||
export const currentUserId = globalSignal(""); | ||
|
||
/** Gets the currently selected user, fallback to first available user */ | ||
export const currentUser = computed<User | undefined>(() => { | ||
return users.value[currentUserId.value] ?? Object.values(users.value)[0]; | ||
}); | ||
/** Currently selected user */ | ||
export const currentUser = globalSignal<User|null>(null); | ||
const isLoadingUser = new Signal<boolean>(false); | ||
|
||
export const isServiceAccount = computed(() => { | ||
return (currentUser.value as ServiceAccountUser)?.type === "service_account"; | ||
}); | ||
|
||
export async function checkLogin() { | ||
const accounts = await getAccounts(); | ||
users.value = accounts.reduce( | ||
(cumm, curr) => ({ ...cumm, [curr.user.email]: curr.user }), | ||
{} | ||
); | ||
return await requireAuthWrapper(); | ||
} | ||
|
||
export function registerUser(broker: ExtensionBrokerImpl, telemetryLogger: TelemetryLogger): Disposable { | ||
|
||
const sub1 = effect(() => { | ||
broker.send("notifyUsers", { users: Object.values(users.value) }); | ||
}); | ||
|
||
const sub2 = effect(() => { | ||
const notifyUserChangedSub = effect(() => { | ||
broker.send("notifyUserChanged", { user: currentUser.value }); | ||
}); | ||
|
||
const sub3 = broker.on("getInitialData", async () => { | ||
checkLogin(); | ||
const getInitialDataSub = broker.on("getInitialData", async () => { | ||
isLoadingUser.value = true; | ||
currentUser.value = await checkLogin(); | ||
isLoadingUser.value = false; | ||
}); | ||
|
||
const sub4 = broker.on("addUser", async () => { | ||
const isLoadingSub = effect(() => { | ||
broker.send("notifyIsLoadingUser", isLoadingUser.value); | ||
}) | ||
|
||
const addUserSub = broker.on("addUser", async () => { | ||
telemetryLogger.logUsage(DATA_CONNECT_EVENT_NAME.LOGIN); | ||
const { user } = await login(); | ||
users.value = { | ||
...users.value, | ||
[user.email]: user, | ||
}; | ||
currentUserId.value = user.email; | ||
currentUser.value = user; | ||
}); | ||
|
||
const sub5 = broker.on("requestChangeUser", ({ user }) => { | ||
currentUserId.value = user.email; | ||
}); | ||
|
||
const sub6 = broker.on("logout", async ({ email }) => { | ||
const logoutSub = broker.on("logout", async ({ email }) => { | ||
try { | ||
await logoutUser(email); | ||
const accounts = await getAccounts(); | ||
users.value = accounts.reduce( | ||
(cumm, curr) => ({ ...cumm, [curr.user.email]: curr.user }), | ||
{} | ||
); | ||
currentUserId.value = ""; | ||
currentUser.value = null; | ||
} catch (e) { | ||
// ignored | ||
} | ||
}); | ||
|
||
return Disposable.from( | ||
{ dispose: sub1 }, | ||
{ dispose: sub2 }, | ||
{ dispose: sub3 }, | ||
{ dispose: sub4 }, | ||
{ dispose: sub5 }, | ||
{ dispose: sub6 } | ||
{ dispose: notifyUserChangedSub }, | ||
{ dispose: getInitialDataSub }, | ||
{ dispose: addUserSub }, | ||
{ dispose: logoutSub }, | ||
{ dispose: isLoadingSub }, | ||
); | ||
} |
12 changes: 6 additions & 6 deletions
12
firebase-vscode/src/test/test_projects/fishfood/dataconnect/dataconnect.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
specVersion: v1alpha | ||
serviceId: us-east | ||
connectorDirs: | ||
- ./connectors/a | ||
specVersion: "v1alpha" | ||
serviceId: "us-east" | ||
location: "europe-north1" | ||
schema: | ||
source: ./schema | ||
source: "./schema" | ||
datasource: | ||
postgresql: | ||
database: "my-database" | ||
database: "emulator" | ||
cloudSql: | ||
instanceId: "dataconnect-test" | ||
connectorDirs: ["./connectors"] |
Oops, something went wrong.