From 798f5b3623c452f360793df75b25589e09614502 Mon Sep 17 00:00:00 2001 From: ekzyis Date: Sat, 9 Sep 2023 22:52:51 +0200 Subject: [PATCH] Move QR code into lib.go --- src/auth.go | 5 +---- src/lib.go | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/auth.go b/src/auth.go index 20e14af..6e78f51 100644 --- a/src/auth.go +++ b/src/auth.go @@ -4,7 +4,6 @@ import ( "crypto/ecdsa" "crypto/rand" "database/sql" - "encoding/base64" "encoding/hex" "fmt" "net/http" @@ -13,7 +12,6 @@ import ( "github.com/btcsuite/btcd/btcec/v2" "github.com/btcsuite/btcutil/bech32" "github.com/labstack/echo/v4" - "github.com/skip2/go-qrcode" ) type LnAuth struct { @@ -84,11 +82,10 @@ func login(c echo.Context) error { } expires := time.Now().Add(60 * 60 * 24 * 365 * time.Second) c.SetCookie(&http.Cookie{Name: "session", HttpOnly: true, Path: "/", Value: sessionId, Secure: true, Expires: expires}) - png, err := qrcode.Encode(lnauth.lnurl, qrcode.Medium, 256) + qr, err := ToQR(lnauth.lnurl) if err != nil { return err } - qr := base64.StdEncoding.EncodeToString([]byte(png)) return c.Render(http.StatusOK, "login.html", map[string]any{"session": c.Get("session"), "PUBLIC_URL": PUBLIC_URL, "lnurl": lnauth.lnurl, "qr": qr}) } diff --git a/src/lib.go b/src/lib.go index 36993b5..8ea9cc9 100644 --- a/src/lib.go +++ b/src/lib.go @@ -1,8 +1,23 @@ package main +import ( + "encoding/base64" + + "github.com/skip2/go-qrcode" +) + func Max(x, y int) int { if x < y { return y } return x } + +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 +}