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 <Base title="D&D Utils"> 6 <script> 7 import "actor-typeahead"; 8 - import { createAuthorizationUrl } from "@atcute/oauth-browser-client"; 9 import { getAuth } from "../../lib/auth"; 10 import metadata from "../../oauth-client-metadata.json" with { type: "json" }; 11 ··· 13 if (localStorage.getItem("did") && (await getAuth().then((x) => !!x[0]))) 14 window.location.assign("/"); 15 16 - const authorize = async (handle: string) => { 17 const target = handle.startsWith("did:") 18 ? (handle as `did:${string}:${string}`) 19 : handle.startsWith("https://") 20 ? (handle as `https://${string}`) 21 : (handle as `${string}.${string}`); 22 23 - const authUrl = await createAuthorizationUrl({ 24 target: !handle.startsWith("https://") 25 ? { 26 type: "account", ··· 32 type: "pds", 33 serviceUrl: target, 34 }, 35 scope: metadata.scope, 36 - }).catch((err) => { 37 - alert(`Error: ${err}`); 38 - console.error(err); 39 - throw err; 40 - }); 41 42 - window.location.assign(authUrl); 43 }; 44 45 const submit = document.getElementById( ··· 62 }); 63 }); 64 65 - const target = new URLSearchParams(window.location.search).get("target"); 66 - if (target) await authorize(target); 67 </script> 68 <style> 69 :global(html, body) {
··· 5 <Base title="D&D Utils"> 6 <script> 7 import "actor-typeahead"; 8 + import { 9 + createAuthorizationUrl, 10 + type AuthorizeOptions, 11 + } from "@atcute/oauth-browser-client"; 12 import { getAuth } from "../../lib/auth"; 13 import metadata from "../../oauth-client-metadata.json" with { type: "json" }; 14 ··· 16 if (localStorage.getItem("did") && (await getAuth().then((x) => !!x[0]))) 17 window.location.assign("/"); 18 19 + const authorize = async ( 20 + handle: string, 21 + create?: boolean, 22 + ): Promise<never> => { 23 const target = handle.startsWith("did:") 24 ? (handle as `did:${string}:${string}`) 25 : handle.startsWith("https://") 26 ? (handle as `https://${string}`) 27 : (handle as `${string}.${string}`); 28 29 + const params: AuthorizeOptions = { 30 target: !handle.startsWith("https://") 31 ? { 32 type: "account", ··· 38 type: "pds", 39 serviceUrl: target, 40 }, 41 + // type cast as atcute doesnt support prompt create yet 42 + prompt: create ? ("create" as unknown as undefined) : undefined, 43 scope: metadata.scope, 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 + ); 59 60 + return window.location.assign(authUrl) as never; 61 }; 62 63 const submit = document.getElementById( ··· 80 }); 81 }); 82 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 + } 95 </script> 96 <style> 97 :global(html, body) {