diff --git a/handler/auth.go b/handler/auth.go index 9bb57cb..71fa20b 100644 --- a/handler/auth.go +++ b/handler/auth.go @@ -7,8 +7,9 @@ import ( "net/http" ) -func handleAuth(db *sql.DB, router *http.ServeMux) { - // Don't use auth middleware for these routes, as it makes redirecting very difficult, if the mail is not yet verified +func authUi(db *sql.DB) http.Handler { + router := http.NewServeMux() + router.Handle("/auth/signin", service.HandleSignInPage(db)) router.Handle("/auth/signup", service.HandleSignUpPage(db)) router.Handle("/auth/verify", service.HandleSignUpVerifyPage(db)) // Hint for the user to verify their email @@ -16,6 +17,14 @@ func handleAuth(db *sql.DB, router *http.ServeMux) { router.Handle("/auth/verify-email", service.HandleSignUpVerifyResponsePage(db)) // The link contained in the email router.Handle("/auth/change-password", service.HandleChangePasswordPage(db)) router.Handle("/auth/reset-password", service.HandleResetPasswordPage(db)) + router.Handle("/", service.HandleIndexAnd404(db)) + + return router +} + +func authApi(db *sql.DB) http.Handler { + router := http.NewServeMux() + router.Handle("/api/auth/signup", service.HandleSignUpComp(db)) router.Handle("/api/auth/signin", service.HandleSignInComp(db)) router.Handle("/api/auth/signout", service.HandleSignOutComp(db)) @@ -24,4 +33,6 @@ func handleAuth(db *sql.DB, router *http.ServeMux) { router.Handle("/api/auth/change-password", service.HandleChangePasswordComp(db)) router.Handle("/api/auth/reset-password", service.HandleResetPasswordComp(db)) router.Handle("/api/auth/reset-password-actual", service.HandleActualResetPasswordComp(db)) + + return router } diff --git a/handler/default.go b/handler/default.go index e877d59..d4194b9 100644 --- a/handler/default.go +++ b/handler/default.go @@ -16,11 +16,17 @@ func GetHandler(db *sql.DB) http.Handler { // Serve static files (CSS, JS and images) router.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./static/")))) - handleWorkout(db, router) + router.Handle("/auth/", authUi(db)) + router.Handle("/api/auth/", authApi(db)) - handleAuth(db, router) + router.Handle("/workout", auth(db, workoutUi(db))) + router.Handle("/api/workout", auth(db, workoutApi(db))) + // Needed a second time with trailing slash, otherwise either /api/workout or /api/workout/{id} does not match + router.Handle("/api/workout/", auth(db, workoutApi(db))) - return middleware.Logging(middleware.EnableCors(router)) + return middleware.Logging( + middleware.EnableCors( + router)) } func auth(db *sql.DB, h http.Handler) http.Handler { diff --git a/handler/workout.go b/handler/workout.go index d78212c..ed3672e 100644 --- a/handler/workout.go +++ b/handler/workout.go @@ -7,9 +7,20 @@ import ( "net/http" ) -func handleWorkout(db *sql.DB, router *http.ServeMux) { - router.Handle("/workout", auth(db, service.HandleWorkoutPage(db))) - router.Handle("POST /api/workout", auth(db, service.HandleWorkoutNewComp(db))) - router.Handle("GET /api/workout", auth(db, service.HandleWorkoutGetComp(db))) - router.Handle("DELETE /api/workout/{id}", auth(db, service.HandleWorkoutDeleteComp(db))) +func workoutUi(db *sql.DB) http.Handler { + router := http.NewServeMux() + + router.Handle("/workout", service.HandleWorkoutPage(db)) + + return router +} + +func workoutApi(db *sql.DB) http.Handler { + router := http.NewServeMux() + + router.Handle("POST /api/workout", service.HandleWorkoutNewComp(db)) + router.Handle("GET /api/workout", service.HandleWorkoutGetComp(db)) + router.Handle("DELETE /api/workout/{id}", service.HandleWorkoutDeleteComp(db)) + + return router } diff --git a/service/auth.go b/service/auth.go index 024ec09..e069846 100644 --- a/service/auth.go +++ b/service/auth.go @@ -585,6 +585,7 @@ func HandleResetPasswordComp(db *sql.DB) http.HandlerFunc { utils.TriggerToast(w, r, "info", "If the email exists, an email has been sent") } } + func sendVerificationEmail(db *sql.DB, userId string, email string) { var token string