2025-05-26 11:19:15 +02:00
|
|
|
import type { CredentialApplication } from "@versia/client/schemas";
|
2024-06-16 00:41:23 +02:00
|
|
|
import { nanoid } from "nanoid";
|
2024-12-07 18:26:52 +01:00
|
|
|
import { toast } from "vue-sonner";
|
2025-05-26 11:19:15 +02:00
|
|
|
import type { z } from "zod";
|
2025-02-09 20:14:27 +01:00
|
|
|
import { confirmModalService } from "~/components/modals/composable";
|
2025-07-16 07:48:39 +02:00
|
|
|
import pkg from "~~/package.json";
|
|
|
|
|
import * as m from "~~/paraglide/messages.js";
|
2024-12-07 18:26:52 +01:00
|
|
|
|
2025-02-09 20:14:27 +01:00
|
|
|
const getRedirectUri = () => new URL("/", useRequestURL().origin);
|
|
|
|
|
|
|
|
|
|
export const askForInstance = async (): Promise<URL> => {
|
|
|
|
|
const { confirmed, value } = await confirmModalService.confirm({
|
|
|
|
|
title: m.sharp_alive_anteater_fade(),
|
|
|
|
|
inputType: "url",
|
|
|
|
|
message: m.noble_misty_rook_slide(),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (confirmed && value) {
|
|
|
|
|
return new URL(URL.canParse(value) ? value : `https://${value}`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw new Error("No instance provided");
|
|
|
|
|
};
|
|
|
|
|
|
2025-01-29 04:39:33 +01:00
|
|
|
export const signIn = async (
|
2025-05-26 11:19:15 +02:00
|
|
|
appData: Ref<z.infer<typeof CredentialApplication> | null>,
|
2025-01-29 04:39:33 +01:00
|
|
|
origin: URL,
|
|
|
|
|
) => {
|
2024-12-07 22:17:22 +01:00
|
|
|
const id = toast.loading(m.level_due_ox_greet());
|
2024-12-07 18:26:52 +01:00
|
|
|
|
2025-01-29 04:39:33 +01:00
|
|
|
const client = useClient(origin);
|
|
|
|
|
|
2025-02-09 20:14:27 +01:00
|
|
|
const redirectUri = getRedirectUri();
|
|
|
|
|
|
|
|
|
|
redirectUri.searchParams.append("origin", origin.toString());
|
2025-01-29 04:39:33 +01:00
|
|
|
|
2025-02-09 20:14:27 +01:00
|
|
|
const output = await client.value.createApp("Versia-FE", {
|
2024-12-07 18:26:52 +01:00
|
|
|
scopes: ["read", "write", "follow", "push"],
|
2025-01-29 04:39:33 +01:00
|
|
|
redirect_uris: redirectUri.toString(),
|
2025-02-09 20:14:27 +01:00
|
|
|
// @ts-expect-error Package.json types are missing this field
|
|
|
|
|
website: pkg.homepage ?? undefined,
|
2024-12-07 18:26:52 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!output?.data) {
|
|
|
|
|
toast.dismiss(id);
|
2024-12-07 22:17:22 +01:00
|
|
|
toast.error(m.silly_sour_fireant_fear());
|
2024-12-07 18:26:52 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
appData.value = output.data;
|
|
|
|
|
|
|
|
|
|
const url = await client.value.generateAuthUrl(
|
|
|
|
|
output.data.client_id,
|
|
|
|
|
output.data.client_secret,
|
|
|
|
|
{
|
|
|
|
|
scopes: ["read", "write", "follow", "push"],
|
2025-01-29 04:39:33 +01:00
|
|
|
redirect_uri: redirectUri.toString(),
|
2024-12-07 18:26:52 +01:00
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (!url) {
|
|
|
|
|
toast.dismiss(id);
|
2024-12-07 22:17:22 +01:00
|
|
|
toast.error(m.candid_frail_lion_value());
|
2024-12-07 18:26:52 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-29 04:39:33 +01:00
|
|
|
// Add "instance_switch_uri" parameter to URL
|
|
|
|
|
const toRedirect = new URL(url);
|
|
|
|
|
|
|
|
|
|
toRedirect.searchParams.append("instance_switch_uri", useRequestURL().href);
|
|
|
|
|
|
|
|
|
|
window.location.href = toRedirect.toString();
|
2024-12-07 18:26:52 +01:00
|
|
|
};
|
2024-06-16 00:41:23 +02:00
|
|
|
|
2025-01-29 04:39:33 +01:00
|
|
|
export const signInWithCode = (
|
|
|
|
|
code: string,
|
2025-05-26 11:19:15 +02:00
|
|
|
appData: z.infer<typeof CredentialApplication>,
|
2025-01-29 04:39:33 +01:00
|
|
|
origin: URL,
|
|
|
|
|
) => {
|
|
|
|
|
const client = useClient(origin);
|
2025-02-09 20:14:27 +01:00
|
|
|
const redirectUri = getRedirectUri();
|
2025-01-29 04:39:33 +01:00
|
|
|
|
2025-02-09 20:14:27 +01:00
|
|
|
redirectUri.searchParams.append("origin", origin.toString());
|
2025-01-29 04:39:33 +01:00
|
|
|
|
2024-06-16 00:41:23 +02:00
|
|
|
client.value
|
|
|
|
|
?.fetchAccessToken(
|
|
|
|
|
appData.client_id,
|
|
|
|
|
appData.client_secret,
|
|
|
|
|
code,
|
2025-01-29 04:39:33 +01:00
|
|
|
redirectUri.toString(),
|
2024-06-16 00:41:23 +02:00
|
|
|
)
|
|
|
|
|
.then(async (res) => {
|
2025-01-29 04:39:33 +01:00
|
|
|
const tempClient = useClient(origin, res.data).value;
|
2024-06-16 00:41:23 +02:00
|
|
|
|
|
|
|
|
const [accountOutput, instanceOutput] = await Promise.all([
|
|
|
|
|
tempClient.verifyAccountCredentials(),
|
|
|
|
|
tempClient.getInstance(),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
// Get account data
|
|
|
|
|
if (
|
|
|
|
|
!identities.value.find(
|
|
|
|
|
(i) => i.account.id === accountOutput.data.id,
|
|
|
|
|
)
|
2024-06-20 02:07:56 +02:00
|
|
|
) {
|
2024-06-16 00:41:23 +02:00
|
|
|
identity.value = {
|
|
|
|
|
id: nanoid(),
|
|
|
|
|
tokens: res.data,
|
|
|
|
|
account: accountOutput.data,
|
|
|
|
|
instance: instanceOutput.data,
|
|
|
|
|
permissions: [],
|
|
|
|
|
emojis: [],
|
|
|
|
|
};
|
2024-06-20 02:07:56 +02:00
|
|
|
}
|
2024-06-16 00:41:23 +02:00
|
|
|
|
|
|
|
|
// Remove code from URL
|
|
|
|
|
window.history.replaceState(
|
|
|
|
|
{},
|
|
|
|
|
document.title,
|
|
|
|
|
window.location.pathname,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Redirect to home
|
|
|
|
|
window.location.pathname = "/";
|
|
|
|
|
});
|
|
|
|
|
};
|
2025-02-09 20:14:27 +01:00
|
|
|
|
|
|
|
|
export const signOut = async (
|
2025-05-26 11:19:15 +02:00
|
|
|
appData: z.infer<typeof CredentialApplication> | null,
|
2025-02-09 20:14:27 +01:00
|
|
|
identityToRevoke: Identity,
|
|
|
|
|
) => {
|
|
|
|
|
const id = toast.loading("Signing out...");
|
|
|
|
|
|
|
|
|
|
if (!appData) {
|
|
|
|
|
toast.dismiss(id);
|
|
|
|
|
toast.error("No app or identity data to sign out");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Don't do anything on error, as Versia Server doesn't implement the revoke endpoint yet
|
|
|
|
|
await client.value
|
|
|
|
|
?.revokeToken(
|
|
|
|
|
appData.client_id,
|
|
|
|
|
identityToRevoke.tokens.access_token,
|
|
|
|
|
identityToRevoke.tokens.access_token,
|
|
|
|
|
)
|
|
|
|
|
.catch(() => {
|
|
|
|
|
// Do nothing
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
identities.value = identities.value.filter(
|
|
|
|
|
(i) => i.id !== identityToRevoke.id,
|
|
|
|
|
);
|
|
|
|
|
toast.dismiss(id);
|
|
|
|
|
toast.success("Signed out");
|
|
|
|
|
};
|