mirror of
https://github.com/versia-pub/frontend.git
synced 2026-03-13 03:29:16 +01:00
refactor: ♻️ Rewrite state system to use Pinia for composer and auth
This commit is contained in:
parent
a6db9e059d
commit
b510782a30
80 changed files with 999 additions and 1011 deletions
|
|
@ -1,13 +1,6 @@
|
|||
import type { CredentialApplication } from "@versia/client/schemas";
|
||||
import { nanoid } from "nanoid";
|
||||
import { toast } from "vue-sonner";
|
||||
import type { z } from "zod";
|
||||
import { confirmModalService } from "~/components/modals/composable";
|
||||
import pkg from "~~/package.json";
|
||||
import * as m from "~~/paraglide/messages.js";
|
||||
|
||||
const getRedirectUri = () => new URL("/", useRequestURL().origin);
|
||||
|
||||
export const askForInstance = async (): Promise<URL> => {
|
||||
const { confirmed, value } = await confirmModalService.confirm({
|
||||
title: m.sharp_alive_anteater_fade(),
|
||||
|
|
@ -21,136 +14,3 @@ export const askForInstance = async (): Promise<URL> => {
|
|||
|
||||
throw new Error("No instance provided");
|
||||
};
|
||||
|
||||
export const signIn = async (
|
||||
appData: Ref<z.infer<typeof CredentialApplication> | null>,
|
||||
origin: URL,
|
||||
) => {
|
||||
const id = toast.loading(m.level_due_ox_greet());
|
||||
|
||||
const client = useClient(origin);
|
||||
|
||||
const redirectUri = getRedirectUri();
|
||||
|
||||
redirectUri.searchParams.append("origin", origin.toString());
|
||||
|
||||
const output = await client.value.createApp("Versia-FE", {
|
||||
scopes: ["read", "write", "follow", "push"],
|
||||
redirect_uris: redirectUri.toString(),
|
||||
// @ts-expect-error Package.json types are missing this field
|
||||
website: pkg.homepage ?? undefined,
|
||||
});
|
||||
|
||||
if (!output?.data) {
|
||||
toast.dismiss(id);
|
||||
toast.error(m.silly_sour_fireant_fear());
|
||||
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: redirectUri.toString(),
|
||||
},
|
||||
);
|
||||
|
||||
if (!url) {
|
||||
toast.dismiss(id);
|
||||
toast.error(m.candid_frail_lion_value());
|
||||
return;
|
||||
}
|
||||
|
||||
// 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();
|
||||
};
|
||||
|
||||
export const signInWithCode = (
|
||||
code: string,
|
||||
appData: z.infer<typeof CredentialApplication>,
|
||||
origin: URL,
|
||||
) => {
|
||||
const client = useClient(origin);
|
||||
const redirectUri = getRedirectUri();
|
||||
|
||||
redirectUri.searchParams.append("origin", origin.toString());
|
||||
|
||||
client.value
|
||||
?.fetchAccessToken(
|
||||
appData.client_id,
|
||||
appData.client_secret,
|
||||
code,
|
||||
redirectUri.toString(),
|
||||
)
|
||||
.then(async (res) => {
|
||||
const tempClient = useClient(origin, 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,
|
||||
)
|
||||
) {
|
||||
identity.value = {
|
||||
id: nanoid(),
|
||||
tokens: res.data,
|
||||
account: accountOutput.data,
|
||||
instance: instanceOutput.data,
|
||||
permissions: [],
|
||||
emojis: [],
|
||||
};
|
||||
}
|
||||
|
||||
// Remove code from URL
|
||||
window.history.replaceState(
|
||||
{},
|
||||
document.title,
|
||||
window.location.pathname,
|
||||
);
|
||||
|
||||
// Redirect to home
|
||||
window.location.pathname = "/";
|
||||
});
|
||||
};
|
||||
|
||||
export const signOut = async (
|
||||
appData: z.infer<typeof CredentialApplication> | null,
|
||||
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");
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,8 +1,10 @@
|
|||
export const wrapUrl = (path: string) => {
|
||||
const authStore = useAuthStore();
|
||||
|
||||
return new URL(
|
||||
path,
|
||||
identity.value
|
||||
? `https://${identity.value.instance.domain}`
|
||||
authStore.instance
|
||||
? `https://${authStore.instance.domain}`
|
||||
: window.location.origin,
|
||||
).toString();
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue