Add QR component

This commit is contained in:
ekzyis 2024-07-15 11:59:32 +02:00
parent db23fbb64a
commit 9b8734f5a9
4 changed files with 30 additions and 30 deletions

View File

@ -1,16 +0,0 @@
package lib
import (
"encoding/base64"
"github.com/skip2/go-qrcode"
)
func ToQR(s string) (string, error) {
png, err := qrcode.Encode(s, qrcode.Medium, 256)
if err != nil {
return "", err
}
qr := base64.StdEncoding.EncodeToString([]byte(png))
return qr, nil
}

View File

@ -5,7 +5,6 @@ import (
"net/http" "net/http"
"time" "time"
"git.ekzyis.com/ekzyis/delphi.market/lib"
"git.ekzyis.com/ekzyis/delphi.market/server/auth" "git.ekzyis.com/ekzyis/delphi.market/server/auth"
"git.ekzyis.com/ekzyis/delphi.market/server/router/context" "git.ekzyis.com/ekzyis/delphi.market/server/router/context"
"git.ekzyis.com/ekzyis/delphi.market/server/router/pages" "git.ekzyis.com/ekzyis/delphi.market/server/router/pages"
@ -41,7 +40,6 @@ func LnAuth(sc context.Context, c echo.Context, action string) error {
sessionId string sessionId string
// sessions expire in 30 days. TODO: refresh sessions // sessions expire in 30 days. TODO: refresh sessions
expires = time.Now().Add(60 * 60 * 24 * 30 * time.Second) expires = time.Now().Add(60 * 60 * 24 * 30 * time.Second)
qr string
err error err error
) )
@ -56,10 +54,6 @@ func LnAuth(sc context.Context, c echo.Context, action string) error {
return err return err
} }
if qr, err = lib.ToQR(lnAuth.LNURL); err != nil {
return err
}
c.SetCookie(&http.Cookie{ c.SetCookie(&http.Cookie{
Name: "session", Name: "session",
HttpOnly: true, HttpOnly: true,
@ -69,7 +63,7 @@ func LnAuth(sc context.Context, c echo.Context, action string) error {
Expires: expires, Expires: expires,
}) })
return pages.LnAuth(qr, lnAuth.LNURL, mapAction(action)).Render(context.RenderContext(sc, c), c.Response().Writer) return pages.LnAuth(lnAuth.LNURL, mapAction(action)).Render(context.RenderContext(sc, c), c.Response().Writer)
} }
func HandleLnAuthCallback(sc context.Context) echo.HandlerFunc { func HandleLnAuthCallback(sc context.Context) echo.HandlerFunc {

View File

@ -0,0 +1,27 @@
package components
import (
"encoding/base64"
"github.com/skip2/go-qrcode"
)
templ Qr(value string, href string) {
if href != "" {
<a
class="mx-auto no-link"
href={ templ.SafeURL(href) }
>
<img src={ "data:image/jpeg;base64," + qrEncode(value) }/>
</a>
} else {
<img src={ "data:image/jpeg;base64," + qrEncode(value) }/>
}
}
func qrEncode(value string) string {
png, err := qrcode.Encode(value, qrcode.Medium, 256)
if err != nil {
return ""
}
return base64.StdEncoding.EncodeToString([]byte(png))
}

View File

@ -2,7 +2,7 @@ package pages
import "git.ekzyis.com/ekzyis/delphi.market/server/router/pages/components" import "git.ekzyis.com/ekzyis/delphi.market/server/router/pages/components"
templ LnAuth(qr string, lnurl string, action string) { templ LnAuth(lnurl string, action string) {
<html> <html>
@components.Head() @components.Head()
<body class="container"> <body class="container">
@ -17,12 +17,7 @@ templ LnAuth(qr string, lnurl string, action string) {
hx-select="#content" hx-select="#content"
hx-push-url="true" hx-push-url="true"
> >
<a @components.Qr(lnurl, "lightning:"+lnurl)
class="mx-auto no-link"
href={ templ.SafeURL("lightning:" + lnurl) }
>
<img src={ "data:image/jpeg;base64," + qr }/>
</a>
<small class="mx-auto w-[256px] my-1 break-words">{ lnurl }</small> <small class="mx-auto w-[256px] my-1 break-words">{ lnurl }</small>
</div> </div>
<div hx-get="/session" hx-trigger="every 1s" hx-swap="none"></div> <div hx-get="/session" hx-trigger="every 1s" hx-swap="none"></div>