diff --git a/frontend/src/components/Settings/SteamLibrarySection.test.ts b/frontend/src/components/Settings/SteamLibrarySection.test.ts index 50bae8067..04db59c78 100644 --- a/frontend/src/components/Settings/SteamLibrarySection.test.ts +++ b/frontend/src/components/Settings/SteamLibrarySection.test.ts @@ -23,8 +23,11 @@ vi.mock("@/services/api/user", () => ({ vi.mock("@/composables/useSnackbar", () => ({ useSnackbar: () => snackbar })); vi.mock("@/composables/useConfirm", () => ({ useConfirm: () => confirm })); vi.mock("@/composables/useSocketEvent", () => ({ useSocketEvent: vi.fn() })); +const routerReplace = vi.fn(); +let routeQuery: Record = {}; vi.mock("vue-router", () => ({ - useRoute: () => ({ fullPath: "/settings/user/1" }), + useRoute: () => ({ fullPath: "/settings/user/1", query: routeQuery }), + useRouter: () => ({ replace: routerReplace }), })); function mountSection() { @@ -51,12 +54,34 @@ function mountSection() { describe("SteamLibrarySection", () => { beforeEach(() => { vi.stubGlobal("location", { ...window.location, assign }); + routeQuery = {}; vi.mocked(userApi.fetchSteamLibrary).mockResolvedValue({ data: [220, 400], } as never); vi.mocked(userApi.syncSteamLibrary).mockResolvedValue({} as never); }); + it("reports a sign-in Steam refused and clears it from the url", async () => { + storeAuth().setCurrentUser(userFixture({ id: 1 })); + routeQuery = { steam_error: "Steam could not confirm the sign-in" }; + + mountSection(); + await flushPromises(); + + expect(snackbar.error).toHaveBeenCalledOnce(); + expect(routerReplace).toHaveBeenCalledWith({ query: {} }); + }); + + it("says nothing when no sign-in just happened", async () => { + storeAuth().setCurrentUser(userFixture({ id: 1 })); + + mountSection(); + await flushPromises(); + + expect(snackbar.error).not.toHaveBeenCalled(); + expect(routerReplace).not.toHaveBeenCalled(); + }); + it("hands the browser to Steam to sign in", async () => { storeAuth().setCurrentUser(userFixture({ id: 1 })); vi.mocked(userApi.startSteamLogin).mockResolvedValue({ diff --git a/frontend/src/components/Settings/SteamLibrarySection.vue b/frontend/src/components/Settings/SteamLibrarySection.vue index 833338cc4..5d73e296f 100644 --- a/frontend/src/components/Settings/SteamLibrarySection.vue +++ b/frontend/src/components/Settings/SteamLibrarySection.vue @@ -2,9 +2,9 @@ // SteamLibrarySection: links a RomM account to a Steam account and imports // the games it owns. The import runs as a task whose completion arrives as a // notification, which also refreshes the owned count shown here. -import { computed, ref } from "vue"; +import { computed, onMounted, ref } from "vue"; import { useI18n } from "vue-i18n"; -import { useRoute } from "vue-router"; +import { useRoute, useRouter } from "vue-router"; import SettingsSection from "@/components/Settings/SettingsSection.vue"; import { useConfirm } from "@/composables/useConfirm"; import { useSnackbar } from "@/composables/useSnackbar"; @@ -18,6 +18,7 @@ defineOptions({ inheritAttrs: false }); const { t } = useI18n(); const route = useRoute(); +const router = useRouter(); const auth = storeAuth(); const steamLibrary = useSteamLibrary(); const snackbar = useSnackbar(); @@ -32,6 +33,23 @@ const personaName = computed( () => auth.user?.steam_persona_name || auth.user?.steam_id || "", ); +// The sign-in happens on Steam's site, so its outcome comes back in the query +// rather than in a reply this component awaited. +onMounted(() => { + const failure = route.query.steam_error; + if (typeof failure === "string" && failure) { + snackbar.error(t("settings.steam-link-failed", { error: failure }), { + icon: "mdi-close-circle", + }); + } + if ("steam_error" in route.query || "steam" in route.query) { + const query = { ...route.query }; + delete query.steam_error; + delete query.steam; + void router.replace({ query }); + } +}); + // Steam signs the user in on its own site, then returns to `next`, so this // hands the browser over rather than awaiting a reply. async function link() {