Skip to content

Commit

Permalink
feat: pass events to window opener
Browse files Browse the repository at this point in the history
  • Loading branch information
im-adithya committed Jul 25, 2024
1 parent eb6c8b4 commit 861da4c
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 7 deletions.
4 changes: 1 addition & 3 deletions echo_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,9 +284,6 @@ func (svc *Service) AppsNewHandler(c echo.Context) error {
sess.Save(c.Request(), c.Response())
return c.Redirect(302, fmt.Sprintf("/%s/auth?c=%s", strings.ToLower(svc.cfg.LNBackendType), appName))
}
if user.HubUrl != "" {
return c.Redirect(302, fmt.Sprintf("%s/#/apps/new?%s", user.HubUrl, c.QueryString()))
}

//construction to return a map with all possible permissions
//and indicate which ones are checked by default in the front-end
Expand All @@ -312,6 +309,7 @@ func (svc *Service) AppsNewHandler(c echo.Context) error {

return c.Render(http.StatusOK, "apps/new.html", map[string]interface{}{
"User": user,
"QueryString": c.QueryString(),
"Name": appName,
"Pubkey": pubkey,
"ReturnTo": returnTo,
Expand Down
53 changes: 49 additions & 4 deletions views/apps/new.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,49 @@
{{define "body"}}

{{if .User.HubUrl}}

<p id="message" class="font-bold text-xl font-headline mb-4 dark:text-white text-center">
Please enable pop-ups and proceed in the new Alby Hub window.<br/>Keep this window open to receive updates.
</p>

<script type="text/javascript">
// Change the page title
document.title = "Waiting for Alby Hub Connection...";

const hubWindow = window.open('{{.User.HubUrl}}/#/apps/new?{{.QueryString}}')

// If you also need to handle postMessage
window.addEventListener("message", (event) => {
if (event.data && event.data.type === "nwc:success") {
// dispatch a success event which can be listened to by the opener or by the app that embeds the webview
// this gives those apps the chance to know the user has enabled the connection
const nwcEvent = new CustomEvent("nwc:success", { detail: {} });
window.dispatchEvent(nwcEvent);

// notify the opener of the successful connection
if (window.opener) {
window.opener.postMessage(
{
type: "nwc:success",
payload: {success: true},
},
"*"
);
}

document.getElementById("message").innerText = "App created successfully"
document.title = "Alby Hub Connection Successful"

// Close the hub window if the flow was started from elsewhere
if (window.opener && hubWindow) {
hubWindow.close();
}
}
});
</script>

{{else}}

<h2 class="font-bold text-2xl font-headline mb-4 dark:text-white">
{{if .Name}}
Connect to {{.Name}}
Expand Down Expand Up @@ -33,12 +77,12 @@ <h2 class="font-bold text-2xl font-headline mb-4 dark:text-white">
Name of the app or purpose of the connection
</p>
{{else}}
<input type="hidden" name="name" id="" value="{{.Name}}">
<input type="hidden" name="name" id="name" value="{{.Name}}">
{{end}}
<input type="hidden" name="RequestMethods" id="request-methods" value={{.RequestMethods}}>
<input type="hidden" name="RequestMethods" id="request-methods" value="{{.RequestMethods}}">
<input type="hidden" name="ExpiresAt" id="expires-at" value="{{.ExpiresAt}}">
<input type="hidden" name="MaxAmount" id="max-amount" value={{if .MaxAmount}}{{.MaxAmount}}{{else}}{{"100000"}}{{end}}>
<input type="hidden" name="BudgetRenewal" id="budget-renewal" value={{if .BudgetRenewal}}{{.BudgetRenewal}}{{else}}{{"monthly"}}{{end}}>
<input type="hidden" name="MaxAmount" id="max-amount" value="{{if .MaxAmount}}{{.MaxAmount}}{{else}}100000{{end}}">
<input type="hidden" name="BudgetRenewal" id="budget-renewal" value="{{if .BudgetRenewal}}{{.BudgetRenewal}}{{else}}monthly{{end}}">
</div>

<div class="flex justify-between items-center mb-2 text-gray-800 dark:text-white">
Expand Down Expand Up @@ -222,3 +266,4 @@ <h2 class="font-bold text-2xl font-headline mb-4 dark:text-white">
})
</script>
{{end}}
{{end}}

0 comments on commit 861da4c

Please sign in to comment.