From 0170d63ae90cd34d3e5b61517453ac5c05052496 Mon Sep 17 00:00:00 2001 From: Tim Wundenberg Date: Thu, 5 Dec 2024 23:46:40 +0100 Subject: [PATCH] tbs --- db/auth.go | 6 +++--- handler/auth.go | 21 +++++++++++---------- handler/index_and_404.go | 2 +- handler/workout.go | 8 ++++---- main.go | 4 +--- 5 files changed, 20 insertions(+), 21 deletions(-) diff --git a/db/auth.go b/db/auth.go index 64533dd..1ab40ad 100644 --- a/db/auth.go +++ b/db/auth.go @@ -352,11 +352,11 @@ func (db AuthSqlite) DeleteToken(token string) error { func (db AuthSqlite) InsertSession(session *Session) error { _, err := db.db.Exec(` - INSERT INTO session (session_id, user_id, created_at) - VALUES (?, ?, ?)`, session.Id, session.UserId, session.CreatedAt) + INSERT INTO session (session_id, user_id, created_at, expires_at) + VALUES (?, ?, ?, ?)`, session.Id, session.UserId, session.CreatedAt, session.ExpiresAt) if err != nil { - log.Error("Could not insert new session", err) + log.Error("Could not insert new session %v", err) return types.ErrInternal } diff --git a/handler/auth.go b/handler/auth.go index 1fdfaa6..6a21061 100644 --- a/handler/auth.go +++ b/handler/auth.go @@ -59,7 +59,7 @@ var ( func (handler AuthImpl) handleSignInPage() http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - session := r.Context().Value(middleware.SessionKey).(*service.Session) + session := middleware.GetSession(r) if session != nil { if !session.User.EmailVerified { utils.DoRedirect(w, r, "/auth/verify") @@ -122,7 +122,7 @@ func (handler AuthImpl) handleSignIn() http.HandlerFunc { func (handler AuthImpl) handleSignUpPage() http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - session := r.Context().Value(middleware.SessionKey).(*service.Session) + session := middleware.GetSession(r) if session != nil { if !session.User.EmailVerified { @@ -140,7 +140,7 @@ func (handler AuthImpl) handleSignUpPage() http.HandlerFunc { func (handler AuthImpl) handleSignUpVerifyPage() http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - session := r.Context().Value(middleware.SessionKey).(*service.Session) + session := middleware.GetSession(r) if session == nil { utils.DoRedirect(w, r, "/auth/signin") return @@ -158,7 +158,7 @@ func (handler AuthImpl) handleSignUpVerifyPage() http.HandlerFunc { func (handler AuthImpl) handleVerifyResendComp() http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - session := r.Context().Value(middleware.SessionKey).(*service.Session) + session := middleware.GetSession(r) if session == nil { utils.DoRedirect(w, r, "/auth/signin") return @@ -221,7 +221,7 @@ func (handler AuthImpl) handleSignUp() http.HandlerFunc { func (handler AuthImpl) handleSignOut() http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - session := r.Context().Value(middleware.SessionKey).(*service.Session) + session := middleware.GetSession(r) if session != nil { err := handler.service.SignOut(session.Id) @@ -248,9 +248,10 @@ func (handler AuthImpl) handleSignOut() http.HandlerFunc { func (handler AuthImpl) handleDeleteAccountPage() http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - session := r.Context().Value(middleware.SessionKey).(*service.Session) + session := middleware.GetSession(r) if session == nil { utils.DoRedirect(w, r, "/auth/signin") + return } comp := auth.DeleteAccountComp() @@ -260,7 +261,7 @@ func (handler AuthImpl) handleDeleteAccountPage() http.HandlerFunc { func (handler AuthImpl) handleDeleteAccountComp() http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - session := r.Context().Value(middleware.SessionKey).(*service.Session) + session := middleware.GetSession(r) if session == nil { utils.DoRedirect(w, r, "/auth/signin") return @@ -289,7 +290,7 @@ func (handler AuthImpl) handleChangePasswordPage() http.HandlerFunc { isPasswordReset := r.URL.Query().Has("token") - session := r.Context().Value(middleware.SessionKey).(*service.Session) + session := middleware.GetSession(r) if session == nil && !isPasswordReset { utils.DoRedirect(w, r, "/auth/signin") @@ -304,7 +305,7 @@ func (handler AuthImpl) handleChangePasswordPage() http.HandlerFunc { func (handler AuthImpl) handleChangePasswordComp() http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - session := r.Context().Value(middleware.SessionKey).(*service.Session) + session := middleware.GetSession(r) if session == nil { utils.DoRedirect(w, r, "/auth/signin") return @@ -326,7 +327,7 @@ func (handler AuthImpl) handleChangePasswordComp() http.HandlerFunc { func (handler AuthImpl) handleResetPasswordPage() http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - session := r.Context().Value(middleware.SessionKey).(*service.Session) + session := middleware.GetSession(r) if session == nil { utils.DoRedirect(w, r, "/auth/signin") return diff --git a/handler/index_and_404.go b/handler/index_and_404.go index 68f3a30..30c67ee 100644 --- a/handler/index_and_404.go +++ b/handler/index_and_404.go @@ -32,7 +32,7 @@ func (handler IndexImpl) Handle(router *http.ServeMux) { func (handler IndexImpl) handleIndexAnd404() http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - session := r.Context().Value(middleware.SessionKey).(*service.Session) + session := middleware.GetSession(r) var user *service.User if session != nil { user = session.User diff --git a/handler/workout.go b/handler/workout.go index 74e1497..ebb563f 100644 --- a/handler/workout.go +++ b/handler/workout.go @@ -39,7 +39,7 @@ func (handler WorkoutImpl) Handle(router *http.ServeMux) { func (handler WorkoutImpl) handleWorkoutPage() http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - session := r.Context().Value(middleware.SessionKey).(*service.Session) + session := middleware.GetSession(r) if session == nil { utils.DoRedirect(w, r, "/auth/signin") return @@ -53,7 +53,7 @@ func (handler WorkoutImpl) handleWorkoutPage() http.HandlerFunc { func (handler WorkoutImpl) handleAddWorkout() http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - session := r.Context().Value(middleware.SessionKey).(*service.Session) + session := middleware.GetSession(r) if session == nil { utils.DoRedirect(w, r, "/auth/signin") return @@ -80,7 +80,7 @@ func (handler WorkoutImpl) handleAddWorkout() http.HandlerFunc { func (handler WorkoutImpl) handleGetWorkout() http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - session := r.Context().Value(middleware.SessionKey).(*service.Session) + session := middleware.GetSession(r) if session == nil { utils.DoRedirect(w, r, "/auth/signin") return @@ -103,7 +103,7 @@ func (handler WorkoutImpl) handleGetWorkout() http.HandlerFunc { func (handler WorkoutImpl) handleDeleteWorkout() http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - session := r.Context().Value(middleware.SessionKey).(*service.Session) + session := middleware.GetSession(r) if session == nil { utils.DoRedirect(w, r, "/auth/signin") return diff --git a/main.go b/main.go index 97e0b67..0ad5eb0 100644 --- a/main.go +++ b/main.go @@ -49,8 +49,7 @@ func run(ctx context.Context, database *sql.DB, env func(string) string) { // init db err := db.RunMigrations(database, "") if err != nil { - log.Error("Could not run migrations: %v", err) - os.Exit(1) + log.Fatal("Could not run migrations: %v", err) } // init servers @@ -81,7 +80,6 @@ func startServer(s *http.Server) { log.Info("Starting server on %v", s.Addr) if err := s.ListenAndServe(); err != nil && err != http.ErrServerClosed { log.Error("error listening and serving: %v", err) - os.Exit(1) } }