mirror of
https://github.com/versia-pub/frontend.git
synced 2026-03-13 11:39:16 +01:00
refactor: 🛂 Refactor account switching to ask for instance
Some checks failed
CodeQL / Analyze (javascript) (push) Failing after 54s
Deploy to GitHub Pages / build (push) Failing after 1m45s
Deploy to GitHub Pages / deploy (push) Has been skipped
Docker / build (push) Failing after 17s
Mirror to Codeberg / Mirror (push) Failing after 1s
Some checks failed
CodeQL / Analyze (javascript) (push) Failing after 54s
Deploy to GitHub Pages / build (push) Failing after 1m45s
Deploy to GitHub Pages / deploy (push) Has been skipped
Docker / build (push) Failing after 17s
Mirror to Codeberg / Mirror (push) Failing after 1s
This commit is contained in:
parent
7e9ccbc932
commit
2e67e7858f
9 changed files with 71 additions and 104 deletions
|
|
@ -2,24 +2,43 @@ import type { Client } from "@versia/client";
|
|||
import type { ApplicationData } from "@versia/client/types";
|
||||
import { nanoid } from "nanoid";
|
||||
import { toast } from "vue-sonner";
|
||||
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(),
|
||||
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");
|
||||
};
|
||||
|
||||
export const signIn = async (
|
||||
appData: Ref<ApplicationData | null>,
|
||||
origin: URL,
|
||||
) => {
|
||||
const id = toast.loading(m.level_due_ox_greet());
|
||||
|
||||
const redirectUri = new URL("/", useRequestURL().origin);
|
||||
|
||||
const client = useClient(origin);
|
||||
|
||||
redirectUri.searchParams.append("origin", client.value.url.origin);
|
||||
const redirectUri = getRedirectUri();
|
||||
|
||||
const output = await client.value.createApp("Versia", {
|
||||
redirectUri.searchParams.append("origin", origin.toString());
|
||||
|
||||
const output = await client.value.createApp("Versia-FE", {
|
||||
scopes: ["read", "write", "follow", "push"],
|
||||
redirect_uris: redirectUri.toString(),
|
||||
website: useBaseUrl().value,
|
||||
// @ts-expect-error Package.json types are missing this field
|
||||
website: pkg.homepage ?? undefined,
|
||||
});
|
||||
|
||||
if (!output?.data) {
|
||||
|
|
@ -59,9 +78,9 @@ export const signInWithCode = (
|
|||
origin: URL,
|
||||
) => {
|
||||
const client = useClient(origin);
|
||||
const redirectUri = new URL("/", useRequestURL().origin);
|
||||
const redirectUri = getRedirectUri();
|
||||
|
||||
redirectUri.searchParams.append("origin", client.value.url.origin);
|
||||
redirectUri.searchParams.append("origin", origin.toString());
|
||||
|
||||
client.value
|
||||
?.fetchAccessToken(
|
||||
|
|
@ -105,3 +124,33 @@ export const signInWithCode = (
|
|||
window.location.pathname = "/";
|
||||
});
|
||||
};
|
||||
|
||||
export const signOut = async (
|
||||
appData: ApplicationData | 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");
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue