cpolis/cmd/frontend/sessions.go

110 lines
2.9 KiB
Go
Raw Normal View History

package frontend
2024-03-03 09:16:49 +01:00
import (
2024-03-03 13:56:49 +01:00
"fmt"
2024-03-03 09:16:49 +01:00
"html/template"
"log"
"net/http"
b "streifling.com/jason/cpolis/cmd/backend"
2024-03-03 09:16:49 +01:00
)
func saveSession(w http.ResponseWriter, r *http.Request, s *b.CookieStore, u *b.User) error {
2024-03-03 13:56:49 +01:00
session, err := s.Get(r, "cookie")
if err != nil {
return fmt.Errorf("error getting session: %v", err)
}
session.Values["authenticated"] = true
session.Values["id"] = u.ID
session.Values["name"] = u.FirstName + u.LastName
session.Values["role"] = u.Role
if err := session.Save(r, w); err != nil {
return fmt.Errorf("error saving session: %v", err)
}
return nil
}
func HomePage(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
2024-03-03 09:16:49 +01:00
return func(w http.ResponseWriter, r *http.Request) {
2024-03-09 11:06:03 +01:00
numRows, err := db.CountEntries("users")
2024-03-03 09:16:49 +01:00
if err != nil {
log.Fatalln(err)
}
files := []string{c.WebDir + "/templates/index.html"}
2024-03-03 09:16:49 +01:00
if numRows == 0 {
files = append(files, c.WebDir+"/templates/first-user.html")
2024-03-03 09:16:49 +01:00
tmpl, err := template.ParseFiles(files...)
template.Must(tmpl, err).Execute(w, nil)
} else {
session, _ := s.Get(r, "cookie")
if auth, ok := session.Values["authenticated"].(bool); auth && ok {
files = append(files, c.WebDir+"/templates/hub.html")
2024-03-03 09:16:49 +01:00
tmpl, err := template.ParseFiles(files...)
template.Must(tmpl, err).Execute(w, session.Values["role"])
} else {
files = append(files, c.WebDir+"/templates/login.html")
2024-03-03 09:16:49 +01:00
tmpl, err := template.ParseFiles(files...)
template.Must(tmpl, err).Execute(w, nil)
}
}
}
}
func Login(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
2024-03-03 09:16:49 +01:00
return func(w http.ResponseWriter, r *http.Request) {
userName := r.PostFormValue("username")
password := r.PostFormValue("password")
2024-03-11 21:08:27 +01:00
id, ok := db.GetID(userName)
if !ok {
http.Error(w, fmt.Sprintf("no such user: %v", userName), http.StatusInternalServerError)
2024-03-03 09:16:49 +01:00
return
}
if err := db.CheckPassword(id, password); err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
user, err := db.GetUser(id)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
2024-03-03 13:56:49 +01:00
if err := saveSession(w, r, s, user); err != nil {
2024-03-03 09:16:49 +01:00
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
tmpl, err := template.ParseFiles(c.WebDir + "/templates/hub.html")
2024-03-03 09:16:49 +01:00
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", user.Role)
}
}
2024-03-12 20:27:39 +01:00
func Logout(c *b.Config, s *b.CookieStore) http.HandlerFunc {
2024-03-12 20:27:39 +01:00
return func(w http.ResponseWriter, r *http.Request) {
session, err := getSession(w, r, c, s)
2024-03-12 20:27:39 +01:00
if err != nil {
return
2024-03-12 20:27:39 +01:00
}
session.Options.MaxAge = -1
if err = session.Save(r, w); err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
tmpl, err := template.ParseFiles(c.WebDir + "/templates/login.html")
2024-03-12 20:27:39 +01:00
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", nil)
}
}