1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
package handler
import (
"html/template"
"log/slog"
"net/http"
"strconv"
"time"
"git.leonardobishop.net/instancer/pkg/session"
)
func GetAuth(tmpl *template.Template) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
tmpl.ExecuteTemplate(w, "auth.html", nil)
}
}
func PostAuth(tmpl *template.Template, session *session.MemoryStore) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
tmpl.ExecuteTemplate(w, "f_auth_error.html", struct {
Message string
}{
Message: "Invalid form data",
})
return
}
team := r.FormValue("team")
if team == "" {
tmpl.ExecuteTemplate(w, "f_auth_error.html", struct {
Message string
}{
Message: "No team entered",
})
return
}
if _, err := strconv.Atoi(team); err != nil {
tmpl.ExecuteTemplate(w, "f_auth_error.html", struct {
Message string
}{
Message: "Team ID must be number",
})
return
}
session, err := session.Create(team)
if err != nil {
slog.Error("could not create session", "cause", err)
tmpl.ExecuteTemplate(w, "f_auth_error.html", struct {
Message string
}{
Message: "Could not create session",
})
return
}
http.SetCookie(w, &http.Cookie{
Name: "session",
Value: session.Token,
Path: "/",
Secure: true,
SameSite: http.SameSiteStrictMode,
HttpOnly: true,
})
w.Header().Add("HX-Redirect", "/")
}
}
func GetLogout(session *session.MemoryStore) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
//TODO expire session here
http.SetCookie(w, &http.Cookie{
Name: "session",
Value: "",
Expires: time.Unix(0, 0),
Path: "/",
Secure: true,
SameSite: http.SameSiteStrictMode,
HttpOnly: true,
})
http.Redirect(w, r, "/auth", http.StatusFound)
}
}
|