From b7d22fa9055712d365a6cd89ab13daccb3999632 Mon Sep 17 00:00:00 2001 From: Jesse Wierzbinski Date: Sat, 7 Dec 2024 18:26:52 +0100 Subject: [PATCH] feat: :sparkles: Properly show an error when accessing authenticated routes while signed out --- components/sidebars/account-switcher.vue | 37 ++---------------------- layouts/app.vue | 27 ++++++++++++++++- utils/auth.ts | 36 +++++++++++++++++++++++ 3 files changed, 64 insertions(+), 36 deletions(-) diff --git a/components/sidebars/account-switcher.vue b/components/sidebars/account-switcher.vue index a948d9f..1550f45 100644 --- a/components/sidebars/account-switcher.vue +++ b/components/sidebars/account-switcher.vue @@ -28,7 +28,7 @@ }} - + Add account @@ -78,40 +78,7 @@ import { SidebarMenuButton } from "../ui/sidebar"; const appData = useAppData(); -const signIn = async () => { - const id = toast.loading("Signing in..."); - - const output = await client.value.createApp("Versia", { - scopes: ["read", "write", "follow", "push"], - redirect_uris: new URL("/", useRequestURL().origin).toString(), - website: useBaseUrl().value, - }); - - if (!output?.data) { - toast.dismiss(id); - toast.error("Failed to create app"); - return; - } - - appData.value = output.data; - - const url = await client.value.generateAuthUrl( - output.data.client_id, - output.data.client_secret, - { - scopes: ["read", "write", "follow", "push"], - redirect_uri: new URL("/", useRequestURL().origin).toString(), - }, - ); - - if (!url) { - toast.dismiss(id); - toast.error("Failed to generate auth URL"); - return; - } - - window.location.href = url; -}; +const signInAction = () => signIn(appData); const signOut = async (userId?: string) => { const id = toast.loading("Signing out..."); diff --git a/layouts/app.vue b/layouts/app.vue index 64cec35..d2c505e 100644 --- a/layouts/app.vue +++ b/layouts/app.vue @@ -1,7 +1,20 @@ @@ -10,7 +23,17 @@ import ComposerDialog from "~/components/composer/dialog.vue"; import SquarePattern from "~/components/graphics/square-pattern.vue"; import Sidebar from "~/components/sidebars/sidebar.vue"; +import { Button } from "~/components/ui/button"; +import { + Card, + CardDescription, + CardFooter, + CardHeader, + CardTitle, +} from "~/components/ui/card"; +const appData = useAppData(); +const signInAction = () => signIn(appData); const { n } = useMagicKeys(); const activeElement = useActiveElement(); const notUsingInput = computed( @@ -19,6 +42,8 @@ const notUsingInput = computed( activeElement.value?.tagName !== "TEXTAREA", ); +const route = useRoute(); + watchEffect(async () => { if (n?.value && notUsingInput.value) { // Wait 50ms diff --git a/utils/auth.ts b/utils/auth.ts index 15b6269..174c799 100644 --- a/utils/auth.ts +++ b/utils/auth.ts @@ -1,5 +1,41 @@ import type { ApplicationData } from "@versia/client/types"; import { nanoid } from "nanoid"; +import { toast } from "vue-sonner"; + +export const signIn = async (appData: Ref) => { + const id = toast.loading("Signing in..."); + + const output = await client.value.createApp("Versia", { + scopes: ["read", "write", "follow", "push"], + redirect_uris: new URL("/", useRequestURL().origin).toString(), + website: useBaseUrl().value, + }); + + if (!output?.data) { + toast.dismiss(id); + toast.error("Failed to create app"); + return; + } + + appData.value = output.data; + + const url = await client.value.generateAuthUrl( + output.data.client_id, + output.data.client_secret, + { + scopes: ["read", "write", "follow", "push"], + redirect_uri: new URL("/", useRequestURL().origin).toString(), + }, + ); + + if (!url) { + toast.dismiss(id); + toast.error("Failed to generate auth URL"); + return; + } + + window.location.href = url; +}; export const signInWithCode = (code: string, appData: ApplicationData) => { client.value