oauth-like support

This commit is contained in:
Pablo Fernandez 2024-01-02 13:02:51 +00:00
parent 712c9cda81
commit 0c2e95f0c4
3 changed files with 25 additions and 6 deletions

View File

@ -51,7 +51,7 @@
"@noble/ciphers": "^0.4.0",
"@noble/curves": "^1.2.0",
"@noble/hashes": "^1.3.2",
"@nostr-dev-kit/ndk": "^2.2.0",
"@nostr-dev-kit/ndk": "^2.3.1",
"@scure/base": "^1.1.3",
"classnames": "^2.3.2",
"compressorjs": "^1.2.1",
@ -77,6 +77,7 @@
"svelte-range-slider-pips": "^2.2.3",
"svelte-switch": "^0.0.5",
"throttle-debounce": "^5.0.0",
"tippy.js": "^6.3.7"
"tippy.js": "^6.3.7",
"uuid": "^9.0.1"
}
}

View File

@ -7,10 +7,11 @@
import Heading from "src/partials/Heading.svelte"
import {isKeyValid, loginWithNsecBunker} from "src/engine"
import {boot} from "src/app/state"
import { nip05 } from "nostr-tools"
let input = ""
const parse = () => {
const parse = async () => {
const r = {pubkey: "", relay: "", token: ""}
if (input.startsWith("bunker://")) {
@ -22,6 +23,13 @@
} catch {
// pass
}
} else if (input.match(/@/)) {
const profile = await nip05.queryProfile(input);
if (profile) {
r.pubkey = profile.pubkey;
} else {
toast.show("error", "Sorry, but that's an invalid public key.")
}
} else {
const [npub, token] = input.split("#")
r.pubkey = npub.startsWith("npub") ? toHex(npub) : npub
@ -31,13 +39,13 @@
return r
}
const logIn = () => {
const params = parse()
const logIn = async () => {
const params = await parse()
if (isKeyValid(params.pubkey)) {
loginWithNsecBunker(params.pubkey, params.token, params.relay)
boot()
} else {
toast.show("error", "Sorry, but that's an invalid public key.")
toast.show("error", "Sorry, but that's an invalid public key.")
}
}
</script>

View File

@ -12,6 +12,16 @@ export const prepareNdk = ({pubkey, bunkerKey, bunkerToken, bunkerRelay}) => {
})
const nip46Signer = new NDKNip46Signer(instance, pubkey, localSigner)
nip46Signer.on("authUrl", (url) => {
const popup = window.open(url, "bunker-auth", "width=600,height=600");
if (!popup) {
// parse the url and add a callbackUrl with the current domain and /bunker-callback
const urlObj = new URL(url)
urlObj.searchParams.set("callbackUrl", `${window.location.origin}/bunker-callback`)
window.location.href = urlObj.toString()
}
})
nip46Signer.token = bunkerToken