this repo has no description

support &create for target login urls, falling back to default if unsupported

vielle.dev 924ec011 acd51d8c

verified
+39 -11
+39 -11
src/pages/atproto/login.astro
··· 5 5 <Base title="D&D Utils"> 6 6 <script> 7 7 import "actor-typeahead"; 8 - import { createAuthorizationUrl } from "@atcute/oauth-browser-client"; 8 + import { 9 + createAuthorizationUrl, 10 + type AuthorizeOptions, 11 + } from "@atcute/oauth-browser-client"; 9 12 import { getAuth } from "../../lib/auth"; 10 13 import metadata from "../../oauth-client-metadata.json" with { type: "json" }; 11 14 ··· 13 16 if (localStorage.getItem("did") && (await getAuth().then((x) => !!x[0]))) 14 17 window.location.assign("/"); 15 18 16 - const authorize = async (handle: string) => { 19 + const authorize = async ( 20 + handle: string, 21 + create?: boolean, 22 + ): Promise<never> => { 17 23 const target = handle.startsWith("did:") 18 24 ? (handle as `did:${string}:${string}`) 19 25 : handle.startsWith("https://") 20 26 ? (handle as `https://${string}`) 21 27 : (handle as `${string}.${string}`); 22 28 23 - const authUrl = await createAuthorizationUrl({ 29 + const params: AuthorizeOptions = { 24 30 target: !handle.startsWith("https://") 25 31 ? { 26 32 type: "account", ··· 32 38 type: "pds", 33 39 serviceUrl: target, 34 40 }, 41 + // type cast as atcute doesnt support prompt create yet 42 + prompt: create ? ("create" as unknown as undefined) : undefined, 35 43 scope: metadata.scope, 36 - }).catch((err) => { 37 - alert(`Error: ${err}`); 38 - console.error(err); 39 - throw err; 40 - }); 44 + }; 45 + const authUrl: URL = await createAuthorizationUrl(params).catch( 46 + async (err): Promise<never> => { 47 + // if its a prompt=create, retry without prompt=create incase thats the issue 48 + // this is stupid code lmfao 49 + if (create) { 50 + console.warn("prompt=create failed"); 51 + return authorize(handle); 52 + } else { 53 + alert(`Error: ${err}`); 54 + console.error(err); 55 + throw err; 56 + } 57 + }, 58 + ); 41 59 42 - window.location.assign(authUrl); 60 + return window.location.assign(authUrl) as never; 43 61 }; 44 62 45 63 const submit = document.getElementById( ··· 62 80 }); 63 81 }); 64 82 65 - const target = new URLSearchParams(window.location.search).get("target"); 66 - if (target) await authorize(target); 83 + // http://localhost:3000/atproto/login?target=https%3A%2F%2Fabnormal.zip%2F&create 84 + const params = new URLSearchParams(window.location.search); 85 + const target = params.get("target"); 86 + const promptCreate = typeof params.get("create") === "string"; 87 + console.log(promptCreate, params.get("create")); 88 + if (target) { 89 + submit.parentElement?.remove(); 90 + let contents = document.createElement("h1"); 91 + contents.innerText = "Resolving..."; 92 + document.body.append(contents); 93 + await authorize(target, promptCreate); 94 + } 67 95 </script> 68 96 <style> 69 97 :global(html, body) {