2024-07-13 13:58:36 +02:00
|
|
|
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"
|
|
|
|
|
2024-07-13 14:09:11 +02:00
|
|
|
b "streifling.com/jason/cpolis/cmd/backend"
|
2024-03-03 09:16:49 +01:00
|
|
|
)
|
|
|
|
|
2024-07-13 14:09:11 +02: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
|
|
|
|
}
|
|
|
|
|
2024-09-28 13:22:53 +02:00
|
|
|
// getSession is used for verifying that the user is logged in and returns their session and an error.
|
2024-09-28 13:44:25 +02:00
|
|
|
func getSession(w http.ResponseWriter, r *http.Request, c *b.Config, s *b.CookieStore) (*b.Session, error) {
|
2024-09-28 13:22:53 +02:00
|
|
|
msg := "Keine gültige Session. Bitte erneut anmelden."
|
|
|
|
tmpl, tmplErr := template.ParseFiles(c.WebDir+"/templates/index.html", c.WebDir+"/templates/login.html")
|
|
|
|
|
2024-09-28 13:44:25 +02:00
|
|
|
tmpSession, err := s.Get(r, "cookie")
|
2024-09-28 13:22:53 +02:00
|
|
|
if err != nil {
|
2024-10-04 10:21:56 +02:00
|
|
|
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)
|
2024-09-28 13:22:53 +02:00
|
|
|
}
|
|
|
|
|
2024-09-28 13:44:25 +02:00
|
|
|
session := &b.Session{Session: *tmpSession}
|
2024-09-28 13:22:53 +02:00
|
|
|
if session.IsNew {
|
2024-10-04 10:21:56 +02:00
|
|
|
if err = template.Must(tmpl, tmplErr).ExecuteTemplate(w, "page-content", msg); err != nil {
|
|
|
|
return nil, fmt.Errorf("error executing template: %v", err)
|
|
|
|
}
|
2024-09-28 13:22:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return session, nil
|
|
|
|
}
|
|
|
|
|
2024-07-13 14:09:11 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2024-10-04 10:11:43 +02:00
|
|
|
files := make([]string, 2)
|
|
|
|
files[0] = c.WebDir + "/templates/index.html"
|
2024-03-03 09:16:49 +01:00
|
|
|
if numRows == 0 {
|
2024-10-04 10:11:43 +02:00
|
|
|
files[1] = c.WebDir + "/templates/first-user.html"
|
2024-03-03 09:16:49 +01:00
|
|
|
tmpl, err := template.ParseFiles(files...)
|
2024-10-04 10:21:56 +02:00
|
|
|
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")
|
2024-10-04 10:35:32 +02:00
|
|
|
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 {
|
2024-10-04 10:11:43 +02:00
|
|
|
files[1] = c.WebDir + "/templates/hub.html"
|
2024-03-03 09:16:49 +01:00
|
|
|
tmpl, err := template.ParseFiles(files...)
|
2024-10-04 10:21:56 +02:00
|
|
|
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 {
|
2024-10-04 10:11:43 +02:00
|
|
|
files[1] = c.WebDir + "/templates/login.html"
|
2024-03-03 09:16:49 +01:00
|
|
|
tmpl, err := template.ParseFiles(files...)
|
2024-10-04 10:21:56 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-13 14:09:11 +02: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")
|
|
|
|
|
2024-03-11 21:08:27 +01:00
|
|
|
id, ok := db.GetID(userName)
|
|
|
|
if !ok {
|
2024-08-30 21:20:29 +02:00
|
|
|
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(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
|
|
|
|
}
|
|
|
|
|
2024-03-29 09:07:17 +01:00
|
|
|
tmpl, err := template.ParseFiles(c.WebDir + "/templates/hub.html")
|
2024-10-04 10:21:56 +02:00
|
|
|
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
|
|
|
|
2024-07-13 14:09:11 +02: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) {
|
2024-08-18 11:20:06 +02:00
|
|
|
session, err := getSession(w, r, c, s)
|
2024-03-12 20:27:39 +01:00
|
|
|
if err != nil {
|
2024-10-04 10:35:32 +02:00
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
2024-08-18 11:20:06 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-03-29 09:07:17 +01:00
|
|
|
tmpl, err := template.ParseFiles(c.WebDir + "/templates/login.html")
|
2024-10-04 10:21:56 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2024-08-18 11:46:23 +02: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 {
|
2024-10-04 10:35:32 +02:00
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
2024-08-18 11:46:23 +02:00
|
|
|
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")
|
2024-10-04 10:21:56 +02:00
|
|
|
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
|
|
|
|
}
|
2024-08-18 11:46:23 +02:00
|
|
|
}
|
|
|
|
}
|