diff --git a/service/auth.go b/service/auth.go index 494229d..d14efec 100644 --- a/service/auth.go +++ b/service/auth.go @@ -71,8 +71,8 @@ func HandleSignUp(db *sql.DB) http.HandlerFunc { user_uuid, err := uuid.NewRandom() if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) log.Printf("Could not generate UUID: %v", err) + auth.Error("Internal Server Error").Render(r.Context(), w) return } @@ -84,11 +84,11 @@ func HandleSignUp(db *sql.DB) http.HandlerFunc { _, err = db.Exec("INSERT INTO user (user_uuid, email, email_verified, is_admin, password, salt, created_at) VALUES (?, ?, FALSE, FALSE, ?, ?, datetime())", user_uuid, email, hash, salt) if err != nil { if strings.Contains(err.Error(), "email") { - http.Error(w, "Bad Request", http.StatusBadRequest) + auth.Error("Bad Request").Render(r.Context(), w) return } - http.Error(w, err.Error(), http.StatusInternalServerError) + auth.Error("Internal Server Error").Render(r.Context(), w) log.Printf("Could not insert user: %v", err) return } @@ -98,7 +98,8 @@ func HandleSignUp(db *sql.DB) http.HandlerFunc { return } - http.Redirect(w, r, "/", http.StatusSeeOther) + w.Header().Add("HX-Redirect", "/") + w.WriteHeader(http.StatusOK) } } @@ -142,9 +143,10 @@ func HandleSignIn(db *sql.DB) http.HandlerFunc { time.Sleep(time.Duration(time_to_wait) * time.Millisecond) if result { - http.Redirect(w, r, "/", http.StatusSeeOther) + w.Header().Add("HX-Redirect", "/") + w.WriteHeader(http.StatusOK) } else { - http.Error(w, "Unauthorized", http.StatusUnauthorized) + auth.Error("Invalid email or password").Render(r.Context(), w) } } } @@ -259,10 +261,9 @@ func verifySessionAndReturnUser(db *sql.DB, r *http.Request) *User { return nil } - // TODO: Test - // if created_at.Add(time.Duration(8 * time.Hour)).Before(time.Now()) { - // return nil - // } + if created_at.Add(time.Duration(8 * time.Hour)).Before(time.Now()) { + return nil + } return &user } diff --git a/service/workout.go b/service/workout.go index b8634fa..edb0b2a 100644 --- a/service/workout.go +++ b/service/workout.go @@ -2,7 +2,7 @@ package service import ( "me-fit/template" - "me-fit/utils" + "me-fit/template/workout" "database/sql" "net/http" @@ -25,7 +25,7 @@ var ( func HandleWorkoutPage(db *sql.DB) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - inner := template.App() + inner := workout.WorkoutComp() user_comp := UserInfoComp(verifySessionAndReturnUser(db, r)) layout := template.Layout(inner, user_comp) layout.Render(r.Context(), w) @@ -80,41 +80,32 @@ func HandleGetWorkouts(db *sql.DB) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { metrics.WithLabelValues("get").Inc() - // token := r.Context().Value(middleware.TOKEN_KEY).(*auth.Token) - // var userId = token.UID - var userId = "" + user := verifySessionAndReturnUser(db, r) + if user == nil { + http.Error(w, "Unauthorized", http.StatusUnauthorized) + return + } - rows, err := db.Query("SELECT rowid, date, type, sets, reps FROM workout WHERE user_id = ?", userId) + rows, err := db.Query("SELECT rowid, date, type, sets, reps FROM workout WHERE user_id = ?", user.user_uuid) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } - var workouts = make([]map[string]interface{}, 0) + var workouts = make([]workout.Workout, 0) for rows.Next() { - var id int - var date string - var workoutType string - var sets int - var reps int + var workout workout.Workout - err = rows.Scan(&id, &date, &workoutType, &sets, &reps) + err = rows.Scan(&workout.Id, &workout.Date, &workout.Type, &workout.Sets, &workout.Reps) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } - workout := map[string]interface{}{ - "id": id, - "date": date, - "type": workoutType, - "sets": sets, - "reps": reps, - } workouts = append(workouts, workout) } - utils.WriteJSON(w, workouts) + workout.WorkoutListComp(workouts).Render(r.Context(), w) } } diff --git a/template/app.templ b/template/app.templ deleted file mode 100644 index 4aec164..0000000 --- a/template/app.templ +++ /dev/null @@ -1,65 +0,0 @@ -package template - -templ App() { -
-
-

Track your workout

- - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-} diff --git a/template/auth/sign_in_or_up.templ b/template/auth/sign_in_or_up.templ index 929ecf9..41ba568 100644 --- a/template/auth/sign_in_or_up.templ +++ b/template/auth/sign_in_or_up.templ @@ -3,11 +3,11 @@ package auth templ SignInOrUp(isSignIn bool) {

@@ -61,5 +61,12 @@ templ SignInOrUp(isSignIn bool) { } + @Error("") } + +templ Error(message string) { +

+ { message } +

+} diff --git a/template/auth/user.templ b/template/auth/user.templ index 0ac8a6c..4b639be 100644 --- a/template/auth/user.templ +++ b/template/auth/user.templ @@ -1,11 +1,10 @@ package auth templ UserComp(user string) { -
+
if user != "" { Sign Out - - { user } +

{ user }

} else { Sign Up Sign In diff --git a/template/workout/workout.templ b/template/workout/workout.templ index c71abcd..7603ad7 100644 --- a/template/workout/workout.templ +++ b/template/workout/workout.templ @@ -1,9 +1,10 @@ package workout -templ Workout() { +templ WorkoutComp() {

Track your workout

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
} + +type Workout struct { + Id string + Date string + Type string + Sets string + Reps string +} + +templ WorkoutListComp(workouts []Workout) { +
+

Workout history

+ + + + + + + + + + + + for _,w := range workouts { + + + + + + + + } + +
DateTypeSetsReps
{ w.Date }{ w.Type }{ w.Sets }{ w.Reps } +
+ +
+
+
+}