Handle session termination display and logout redirection

This commit is contained in:
2024-09-21 19:27:23 +07:00
parent 80941bd3bb
commit 3cc0322aa1
5 changed files with 126 additions and 213 deletions

View File

@ -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
}