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
|
|
|
|
|
|
|
|
export const signIn = async (appData: Ref<ApplicationData | null>) => {
|
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
|
|
|
|
|
|
|
|
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);
|
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"],
|
|
|
|
|
redirect_uri: new URL("/", useRequestURL().origin).toString(),
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
window.location.href = url;
|
|
|
|
|
};
|
2024-06-16 00:41:23 +02:00
|
|
|
|
|
|
|
|
export const signInWithCode = (code: string, appData: ApplicationData) => {
|
|
|
|
|
client.value
|
|
|
|
|
?.fetchAccessToken(
|
|
|
|
|
appData.client_id,
|
|
|
|
|
appData.client_secret,
|
|
|
|
|
code,
|
|
|
|
|
new URL("/", useRequestURL().origin).toString(),
|
|
|
|
|
)
|
|
|
|
|
.then(async (res) => {
|
|
|
|
|
const tempClient = useClient(res.data).value;
|
|
|
|
|
|
|
|
|
|
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 = "/";
|
|
|
|
|
});
|
|
|
|
|
};
|