Refactor session package for better structure and naming conventions

This commit is contained in:
2024-04-26 20:33:34 +07:00
parent 769bffe6ff
commit c43fd0232e
11 changed files with 52 additions and 161 deletions

View File

@ -30,9 +30,9 @@ func GET(w http.ResponseWriter, r *http.Request) {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
storeSession, err := session.Store.Get(cookie.Value)
storeSession, err := session.GlobalSessionStore.Get(cookie.Value)
if err != nil {
if errors.Is(err, &session.SessionNotFound{}) {
if errors.Is(err, &session.SessionNotFoundError{}) {
storeSession.Destroy(w)
}
http.Error(w, err.Error(), http.StatusInternalServerError)

View File

@ -94,7 +94,7 @@ func POST(w http.ResponseWriter, r *http.Request) {
delete(forgotPasswordHandler.ListForgotPassword, data.User.Email)
delete(forgotPasswordHandler.UserForgotPassword, data.Code)
session.RemoveAllSession(data.User.Email)
session.RemoveAllSessions(data.User.Email)
user.DeleteCache(data.User.Email)

View File

@ -21,17 +21,17 @@ func GET(w http.ResponseWriter, r *http.Request) {
return
}
storeSession, err := session.Store.Get(cookie.Value)
storeSession, err := session.GlobalSessionStore.Get(cookie.Value)
if err != nil {
if errors.Is(err, &session.SessionNotFound{}) {
if errors.Is(err, &session.SessionNotFoundError{}) {
storeSession.Destroy(w)
}
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
session.Store.Delete(cookie.Value)
session.RemoveSession(storeSession.Values["user"].(types.User).Email, cookie.Value)
session.GlobalSessionStore.Delete(cookie.Value)
session.RemoveSessionInfo(storeSession.Values["user"].(types.User).Email, cookie.Value)
http.SetCookie(w, &http.Cookie{
Name: utils.Getenv("SESSION_NAME"),

View File

@ -57,7 +57,7 @@ func POST(w http.ResponseWriter, r *http.Request) {
}
if email == userData.Email && utils.CheckPasswordHash(password, userData.Password) {
storeSession := session.Store.Create()
storeSession := session.GlobalSessionStore.Create()
storeSession.Values["user"] = types.User{
UserID: userData.UserID,
Email: email,
@ -79,7 +79,7 @@ func POST(w http.ResponseWriter, r *http.Request) {
}
storeSession.Save(w)
session.AppendSession(email, &sessionInfo)
session.AddSessionInfo(email, &sessionInfo)
cookie, err := r.Cookie("redirect")
if errors.Is(err, http.ErrNoCookie) {

View File

@ -30,9 +30,9 @@ func POST(w http.ResponseWriter, r *http.Request) {
return
}
storeSession, err := session.Store.Get(cookie.Value)
storeSession, err := session.GlobalSessionStore.Get(cookie.Value)
if err != nil {
if errors.Is(err, &session.SessionNotFound{}) {
if errors.Is(err, &session.SessionNotFoundError{}) {
storeSession.Destroy(w)
}
http.Error(w, err.Error(), http.StatusInternalServerError)

View File

@ -44,9 +44,9 @@ func POST(w http.ResponseWriter, r *http.Request) {
return
}
storeSession, err := session.Store.Get(cookie.Value)
storeSession, err := session.GlobalSessionStore.Get(cookie.Value)
if err != nil {
if errors.Is(err, &session.SessionNotFound{}) {
if errors.Is(err, &session.SessionNotFoundError{}) {
storeSession.Destroy(w)
}
http.Error(w, err.Error(), http.StatusInternalServerError)

View File

@ -20,9 +20,9 @@ func GET(w http.ResponseWriter, r *http.Request) {
if err != nil {
return
}
storeSession, err := session.Store.Get(cookie.Value)
storeSession, err := session.GlobalSessionStore.Get(cookie.Value)
if err != nil {
if errors.Is(err, &session.SessionNotFound{}) {
if errors.Is(err, &session.SessionNotFoundError{}) {
storeSession.Destroy(w)
}
http.Error(w, err.Error(), http.StatusInternalServerError)
@ -35,7 +35,7 @@ func GET(w http.ResponseWriter, r *http.Request) {
return
}
component := userView.Main("User Page", userSession.Email, userSession.Username, session.UserSessions[userSession.Email])
component := userView.Main("User Page", userSession.Email, userSession.Username, session.UserSessionInfoList[userSession.Email])
err = component.Render(r.Context(), w)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)