Fix session retrieval issue after migrating to Redis cache

This commit is contained in:
2024-09-08 16:41:33 +07:00
parent 2c5de2b336
commit 29ab28fd93
5 changed files with 30 additions and 42 deletions

View File

@ -37,8 +37,8 @@ func POST(w http.ResponseWriter, r *http.Request) {
totp := gotp.NewDefaultTOTP(user.Totp)
if totp.Verify(code, time.Now().Unix()) {
storeSession, err := session.Get(key)
err = storeSession.Change(types.User{
storeSession := session.Get(key)
err := storeSession.Change(types.User{
UserID: user.UserID,
Email: user.Email,
Username: user.Username,

View File

@ -2,6 +2,7 @@ package logoutHandler
import (
"errors"
"github.com/fossyy/filekeeper/types"
"net/http"
"github.com/fossyy/filekeeper/session"
@ -9,12 +10,12 @@ import (
)
func GET(w http.ResponseWriter, r *http.Request) {
userSession := r.Context().Value("user").(types.User)
cookie, err := r.Cookie("Session")
if err != nil {
return
}
storeSession, err := session.Get(cookie.Value)
storeSession := session.Get(cookie.Value)
if err != nil {
if errors.Is(err, &session.SessionNotFoundError{}) {
storeSession.Destroy(w)
@ -23,8 +24,16 @@ func GET(w http.ResponseWriter, r *http.Request) {
return
}
storeSession.Delete()
session.RemoveSessionInfo(storeSession.Values.Email, cookie.Value)
err = storeSession.Delete()
if err != nil {
panic(err)
return
}
err = session.RemoveSessionInfo(userSession.Email, cookie.Value)
if err != nil {
panic(err)
return
}
http.SetCookie(w, &http.Cookie{
Name: utils.Getenv("SESSION_NAME"),

View File

@ -10,7 +10,7 @@ func DELETE(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("id")
_, mySession, _ := session.GetSession(r)
otherSession, _ := session.Get(id)
otherSession := session.Get(id)
if _, err := session.GetSessionInfo(mySession.Email, otherSession.ID); err != nil {
w.WriteHeader(http.StatusUnauthorized)
return