mirror of
https://github.com/versia-pub/frontend.git
synced 2025-12-06 08:28:20 +01:00
51 lines
1.5 KiB
TypeScript
51 lines
1.5 KiB
TypeScript
import type { ApplicationData } from "@lysand-org/client/types";
|
|
import { nanoid } from "nanoid";
|
|
|
|
export const signInWithCode = (code: string, appData: ApplicationData) => {
|
|
const identity = useCurrentIdentity();
|
|
const identities = useIdentities();
|
|
const client = useClient();
|
|
|
|
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,
|
|
)
|
|
) {
|
|
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 = "/";
|
|
});
|
|
};
|