cpolis/cmd/frontend/sessions.go

185 lines
5.3 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
}
// getSession is used for verifying that the user is logged in and returns their session and an error.
func getSession(w http.ResponseWriter, r *http.Request, c *b.Config, s *b.CookieStore) (*b.Session, error) {
msg := "Keine gültige Session. Bitte erneut anmelden."
tmpl, tmplErr := template.ParseFiles(c.WebDir+"/templates/index.html", c.WebDir+"/templates/login.html")
tmpSession, err := s.Get(r, "cookie")
if err != nil {
if err = template.Must(tmpl, tmplErr).ExecuteTemplate(w, "page-content", msg); err != nil {
return nil, fmt.Errorf("error executing template: %v", err)
}
return nil, fmt.Errorf("error getting session: %v", err)
}
session := &b.Session{Session: *tmpSession}
if session.IsNew {
if err = template.Must(tmpl, tmplErr).ExecuteTemplate(w, "page-content", msg); err != nil {
return nil, fmt.Errorf("error executing template: %v", err)
}
}
return session, 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 := make([]string, 2)
files[0] = c.WebDir + "/templates/index.html"
2024-03-03 09:16:49 +01:00
if numRows == 0 {
files[1] = c.WebDir + "/templates/first-user.html"
2024-03-03 09:16:49 +01:00
tmpl, err := template.ParseFiles(files...)
if err = template.Must(tmpl, err).Execute(w, nil); err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
2024-03-03 09:16:49 +01:00
} else {
2024-10-04 11:51:24 +02:00
session, err := s.Get(r, "cookie")
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
2024-03-03 09:16:49 +01:00
if auth, ok := session.Values["authenticated"].(bool); auth && ok {
files[1] = c.WebDir + "/templates/hub.html"
2024-03-03 09:16:49 +01:00
tmpl, err := template.ParseFiles(files...)
if err = template.Must(tmpl, err).Execute(w, session.Values["role"]); err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
2024-03-03 09:16:49 +01:00
} else {
files[1] = c.WebDir + "/templates/login.html"
2024-03-03 09:16:49 +01:00
tmpl, err := template.ParseFiles(files...)
if err = template.Must(tmpl, err).Execute(w, nil); err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
2024-03-03 09:16:49 +01:00
}
}
}
}
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")
id := db.GetID(userName)
if id == 0 {
http.Error(w, fmt.Sprintf("no such user: %v", userName), http.StatusBadRequest)
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(c, id)
2024-03-03 09:16:49 +01:00
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")
if err = template.Must(tmpl, err).ExecuteTemplate(w, "page-content", user.Role); err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
2024-03-03 09:16:49 +01:00
}
}
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 {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
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")
if err = template.Must(tmpl, err).ExecuteTemplate(w, "page-content", nil); err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
2024-03-12 20:27:39 +01:00
}
}
func ShowHub(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
session, err := getSession(w, r, c, s)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
session.Values["article"] = nil
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/hub.html")
if err = template.Must(tmpl, err).ExecuteTemplate(w, "page-content", session.Values["role"].(int)); err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
}