2025-01-29 04:39:33 +01:00
|
|
|
import type { Client } from "@versia/client";
|
2024-08-28 00:23:29 +02:00
|
|
|
import type { ApplicationData } from "@versia/client/types";
|
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";
|
2024-12-07 22:17:22 +01:00
|
|
|
import * as m from "~/paraglide/messages.js";
|
2024-12-07 18:26:52 +01:00
|
|
|
|
2025-01-29 04:39:33 +01:00
|
|
|
export const signIn = async (
|
|
|
|
|
appData: Ref<ApplicationData | null>,
|
|
|
|
|
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 redirectUri = new URL("/", useRequestURL().origin);
|
|
|
|
|
|
|
|
|
|
const client = useClient(origin);
|
|
|
|
|
|
|
|
|
|
redirectUri.searchParams.append("origin", client.value.url.origin);
|
|
|
|
|
|
2024-12-07 18:26:52 +01:00
|
|
|
const output = await client.value.createApp("Versia", {
|
|
|
|
|
scopes: ["read", "write", "follow", "push"],
|
2025-01-29 04:39:33 +01:00
|
|
|
redirect_uris: redirectUri.toString(),
|
2024-12-07 18:26:52 +01:00
|
|
|
website: useBaseUrl().value,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
|
appData: ApplicationData,
|
|
|
|
|
origin: URL,
|
|
|
|
|
) => {
|
|
|
|
|
const client = useClient(origin);
|
|
|
|
|
const redirectUri = new URL("/", useRequestURL().origin);
|
|
|
|
|
|
|
|
|
|
redirectUri.searchParams.append("origin", client.value.url.origin);
|
|
|
|
|
|
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 = "/";
|
|
|
|
|
});
|
|
|
|
|
};
|