document what I did for oauth-like flow

This commit is contained in:
Pablo Fernandez 2024-01-02 13:49:21 +00:00
parent 0c2e95f0c4
commit 207a0c61fe
2 changed files with 18 additions and 2 deletions

View File

@ -24,6 +24,9 @@
// pass
}
} else if (input.match(/@/)) {
/**
* Check if this is looks like a nip05 profile.
*/
const profile = await nip05.queryProfile(input);
if (profile) {
r.pubkey = profile.pubkey;

View File

@ -12,13 +12,26 @@ export const prepareNdk = ({pubkey, bunkerKey, bunkerToken, bunkerRelay}) => {
})
const nip46Signer = new NDKNip46Signer(instance, pubkey, localSigner)
/**
* When a signer can authorize an event via a URL, an authUrl event is emitted.
*/
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
/**
* If the popup is blocked, we redirect the user to the auth URL and add a callback URL
* that will be called with the pubkey generated by the bunker, where we will complete the
* nip46 authorization (i.e. send a `connect` command)
*/
const urlObj = new URL(url)
urlObj.searchParams.set("callbackUrl", `${window.location.origin}/bunker-callback`)
// Just to ensure we redirect to the root of the app but this could be a "connection successful!" page or something like that
const selfUrl = new URL(window.location.href)
selfUrl.pathname = "/"
urlObj.searchParams.set("callbackUrl", selfUrl.toString());
// Redirect to the auth URL
window.location.href = urlObj.toString()
}
})