diff --git a/handler/user/session/terminate/terminate.go b/handler/user/session/terminate/terminate.go index 66d2d04..c8c0a72 100644 --- a/handler/user/session/terminate/terminate.go +++ b/handler/user/session/terminate/terminate.go @@ -1,48 +1,43 @@ package userSessionTerminateHandler import ( + "errors" "github.com/fossyy/filekeeper/app" "github.com/fossyy/filekeeper/session" "github.com/fossyy/filekeeper/types" - "github.com/fossyy/filekeeper/view/client/user" "net/http" ) func DELETE(w http.ResponseWriter, r *http.Request) { id := r.PathValue("id") mySession := r.Context().Value("user").(types.User) + mySessionID := r.Context().Value("sessionID").(string) + + if id == mySessionID { + w.Header().Set("HX-Redirect", "/logout") + w.WriteHeader(http.StatusOK) + return + } + otherSession := session.Get(id) - if _, err := session.GetSessionInfo(mySession.Email, otherSession.ID); err != nil { - w.WriteHeader(http.StatusUnauthorized) + err := session.RemoveSessionInfo(mySession.Email, otherSession.ID) + if err != nil { + if errors.Is(err, session.ErrorSessionNotFound) { + w.WriteHeader(http.StatusUnauthorized) + return + } + w.WriteHeader(http.StatusInternalServerError) + app.Server.Logger.Error(err.Error()) return } - err := otherSession.Delete() + err = otherSession.Delete() if err != nil { w.WriteHeader(http.StatusInternalServerError) app.Server.Logger.Error(err.Error()) return } - err = session.RemoveSessionInfo(mySession.Email, otherSession.ID) - if err != nil { - w.WriteHeader(http.StatusInternalServerError) - app.Server.Logger.Error(err.Error()) - return - } - - sessions, err := session.GetSessions(mySession.Email) - if err != nil { - w.WriteHeader(http.StatusInternalServerError) - app.Server.Logger.Error(err.Error()) - return - } - component := userView.SessionTable(sessions) - - err = component.Render(r.Context(), w) - if err != nil { - w.WriteHeader(http.StatusInternalServerError) - return - } + w.WriteHeader(http.StatusOK) return } diff --git a/middleware/middleware.go b/middleware/middleware.go index 32bdaf2..7585385 100644 --- a/middleware/middleware.go +++ b/middleware/middleware.go @@ -86,6 +86,7 @@ func Auth(next http.HandlerFunc, w http.ResponseWriter, r *http.Request) { switch status { case session.Authorized: ctx := context.WithValue(r.Context(), "user", user) + ctx = context.WithValue(ctx, "sessionID", sessionID) req := r.WithContext(ctx) next.ServeHTTP(w, req) return diff --git a/session/session.go b/session/session.go index 7803bf8..fc5a2bf 100644 --- a/session/session.go +++ b/session/session.go @@ -16,10 +16,14 @@ import ( "github.com/fossyy/filekeeper/utils" ) +var ErrorSessionNotFound = errors.New("session not found") + type Session struct { ID string } +type UserStatus string + type SessionInfo struct { SessionID string Browser string @@ -30,8 +34,6 @@ type SessionInfo struct { Location string } -type UserStatus string - const ( Authorized UserStatus = "authorized" Unauthorized UserStatus = "unauthorized" @@ -115,6 +117,9 @@ func RemoveSessionInfo(email string, id string) error { key := "UserSessionInfo:" + email + ":" + id err := app.Server.Cache.DeleteCache(context.Background(), key) if err != nil { + if errors.Is(err, redis.Nil) { + return ErrorSessionNotFound + } return err } return nil @@ -150,7 +155,7 @@ func GetSessionInfo(email string, id string) (*SessionInfo, error) { sessionInfoData, err := app.Server.Cache.GetCache(context.Background(), key) if err != nil { if errors.Is(err, redis.Nil) { - return nil, nil + return nil, ErrorSessionNotFound } return nil, err } diff --git a/view/client/user/user.templ b/view/client/user/user.templ index a628937..b2cd295 100644 --- a/view/client/user/user.templ +++ b/view/client/user/user.templ @@ -111,26 +111,28 @@ templ MainContent(title string, user types.User, allowance *types.Allowance, Lis - - for _, ses := range ListSession { - - {ses.IP} - - {ses.Browser + ses.Version} - - {ses.OS + ses.OSVersion} - - - - - + + for _, session := range ListSession { + + {session.IP} + + {session.Browser + session.Version} + + {session.OS + session.OSVersion} + + + + + } @@ -307,31 +309,6 @@ templ MainContent(title string, user types.User, allowance *types.Allowance, Lis