feat: copy user reference link

This commit is contained in:
Teajey 2024-05-13 22:32:31 +12:00
parent 78419357b5
commit 018baa0e0f
Signed by: Teajey
GPG Key ID: 970E790FE834A713
7 changed files with 40 additions and 5 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
.DS_Store .DS_Store
gin-bin gin-bin
lishwist.db lishwist.db
.env

View File

@ -41,7 +41,7 @@ func (auth *AuthMiddleware) ExpectUser(r *http.Request) *db.User {
} }
func NewAuthMiddleware(protectedHandler http.Handler, publicHandler http.Handler) *AuthMiddleware { func NewAuthMiddleware(protectedHandler http.Handler, publicHandler http.Handler) *AuthMiddleware {
store := sessions.NewCookieStore([]byte(env.Secret)) store := sessions.NewCookieStore([]byte(env.JwtSecret))
store.Options.MaxAge = 86_400 store.Options.MaxAge = 86_400
return &AuthMiddleware{store, protectedHandler, publicHandler} return &AuthMiddleware{store, protectedHandler, publicHandler}
} }

View File

@ -39,6 +39,7 @@ func (auth *AuthMiddleware) LoginPost(w http.ResponseWriter, r *http.Request) {
session, err := auth.Store.Get(r, "lishwist_user") session, err := auth.Store.Get(r, "lishwist_user")
if err != nil { if err != nil {
log.Println("Couldn't get jwt:", err)
http.Error(w, "Something went wrong. Error code: Sokka", http.StatusInternalServerError) http.Error(w, "Something went wrong. Error code: Sokka", http.StatusInternalServerError)
return return
} }

View File

@ -4,6 +4,7 @@ import (
"net/http" "net/http"
"lishwist/db" "lishwist/db"
"lishwist/env"
"lishwist/templates" "lishwist/templates"
) )
@ -12,6 +13,7 @@ type HomeProps struct {
Gifts []db.Gift Gifts []db.Gift
Todo []db.Gift Todo []db.Gift
Reference string Reference string
HostUrl string
} }
func (ctx *Context) Home(w http.ResponseWriter, r *http.Request) { func (ctx *Context) Home(w http.ResponseWriter, r *http.Request) {
@ -26,6 +28,6 @@ func (ctx *Context) Home(w http.ResponseWriter, r *http.Request) {
http.Error(w, "An error occurred while fetching your wishlist :(", http.StatusInternalServerError) http.Error(w, "An error occurred while fetching your wishlist :(", http.StatusInternalServerError)
return return
} }
p := HomeProps{Username: user.Name, Gifts: gifts, Todo: todo, Reference: user.Reference} p := HomeProps{Username: user.Name, Gifts: gifts, Todo: todo, Reference: user.Reference, HostUrl: env.HostUrl.String()}
templates.Execute(w, "home.gotmpl", p) templates.Execute(w, "home.gotmpl", p)
} }

29
env/env.go vendored
View File

@ -1,3 +1,30 @@
package env package env
const Secret = "BLAHBLAHLBAH" import (
"log"
"net/url"
"os"
)
func GuaranteeEnv(key string) (variable string) {
variable, ok := os.LookupEnv(key)
if !ok || variable == "" {
log.Fatalln("Missing environment variable:", key)
}
return
}
var JwtSecret = GuaranteeEnv("LISHWIST_JWT_SECRET")
var HostDomain = GuaranteeEnv("LISHWIST_DOMAIN")
var HostPort = os.Getenv("LISHWIST_PORT")
var HostUrl = func() *url.URL {
rawUrl := "http://" + HostDomain
if HostPort != "" {
rawUrl += ":" + HostPort
}
u, err := url.Parse(rawUrl)
if err != nil {
log.Fatalln("Couldn't parse host url:", err)
}
return u
}()

View File

@ -7,6 +7,7 @@ import (
"lishwist/auth" "lishwist/auth"
"lishwist/context" "lishwist/context"
"lishwist/db" "lishwist/db"
"lishwist/env"
"lishwist/templates" "lishwist/templates"
) )
@ -44,5 +45,5 @@ func main() {
http.Handle("/", authMiddleware) http.Handle("/", authMiddleware)
http.ListenAndServe(":4000", nil) http.ListenAndServe(":"+env.HostPort, nil)
} }

View File

@ -6,8 +6,11 @@
<h1>Lishwist</h1> <h1>Lishwist</h1>
<dl> <dl>
<dt>User reference:</dt> <dt>User reference:</dt>
<dd> <dd style="display: flex; gap: 0.5rem">
<pre>{{.Reference}}</pre> <pre>{{.Reference}}</pre>
<div style="display: flex; align-items: center;">
<button onclick="navigator.clipboard.writeText('{{.HostUrl}}/{{.Reference}}');">Copy link</button>
</div>
</dd> </dd>
</dl> </dl>
<section> <section>