From c43fd0232e1536655cc49f4bb0ebf02dc011b916 Mon Sep 17 00:00:00 2001 From: Bagas Aulia Rezki Date: Fri, 26 Apr 2024 20:33:34 +0700 Subject: [PATCH] Refactor session package for better structure and naming conventions --- handler/download/download.go | 4 +- handler/forgotPassword/verify/verify.go | 2 +- handler/logout/logout.go | 8 +- handler/signin/signin.go | 4 +- .../upload/initialisation/initialisation.go | 4 +- handler/upload/upload.go | 4 +- handler/user/user.go | 6 +- middleware/middleware.go | 8 +- public/index.html | 101 +----------------- routes/routes.go | 6 -- session/session.go | 66 ++++++------ 11 files changed, 52 insertions(+), 161 deletions(-) diff --git a/handler/download/download.go b/handler/download/download.go index c8da242..c612e5b 100644 --- a/handler/download/download.go +++ b/handler/download/download.go @@ -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) diff --git a/handler/forgotPassword/verify/verify.go b/handler/forgotPassword/verify/verify.go index e577ba5..e5b3a9c 100644 --- a/handler/forgotPassword/verify/verify.go +++ b/handler/forgotPassword/verify/verify.go @@ -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) diff --git a/handler/logout/logout.go b/handler/logout/logout.go index 6c6fd2f..4eda278 100644 --- a/handler/logout/logout.go +++ b/handler/logout/logout.go @@ -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"), diff --git a/handler/signin/signin.go b/handler/signin/signin.go index ef8a55a..c6d52e4 100644 --- a/handler/signin/signin.go +++ b/handler/signin/signin.go @@ -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) { diff --git a/handler/upload/initialisation/initialisation.go b/handler/upload/initialisation/initialisation.go index 630f5cd..a5026f1 100644 --- a/handler/upload/initialisation/initialisation.go +++ b/handler/upload/initialisation/initialisation.go @@ -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) diff --git a/handler/upload/upload.go b/handler/upload/upload.go index c3fa1b7..83f6003 100644 --- a/handler/upload/upload.go +++ b/handler/upload/upload.go @@ -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) diff --git a/handler/user/user.go b/handler/user/user.go index a11d2e3..07ad7f7 100644 --- a/handler/user/user.go +++ b/handler/user/user.go @@ -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) diff --git a/middleware/middleware.go b/middleware/middleware.go index 4b94b3e..dde1de0 100644 --- a/middleware/middleware.go +++ b/middleware/middleware.go @@ -74,10 +74,10 @@ func Auth(next http.HandlerFunc, 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.Redirect(w, r, "/signin", http.StatusSeeOther) return @@ -112,9 +112,9 @@ func Guest(next http.HandlerFunc, 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{}) { http.SetCookie(w, &http.Cookie{ Name: "Session", Value: "", diff --git a/public/index.html b/public/index.html index 06b673b..96de0db 100644 --- a/public/index.html +++ b/public/index.html @@ -110,106 +110,7 @@ diff --git a/routes/routes.go b/routes/routes.go index 58fdb63..4c5646c 100644 --- a/routes/routes.go +++ b/routes/routes.go @@ -1,7 +1,6 @@ package routes import ( - "encoding/json" downloadHandler "github.com/fossyy/filekeeper/handler/download" downloadFileHandler "github.com/fossyy/filekeeper/handler/download/file" forgotPasswordHandler "github.com/fossyy/filekeeper/handler/forgotPassword" @@ -16,7 +15,6 @@ import ( "github.com/fossyy/filekeeper/handler/upload/initialisation" userHandler "github.com/fossyy/filekeeper/handler/user" "github.com/fossyy/filekeeper/middleware" - "github.com/fossyy/filekeeper/session" "net/http" ) @@ -37,10 +35,6 @@ func SetupRoutes() *http.ServeMux { } }) - handler.HandleFunc("/test", func(w http.ResponseWriter, r *http.Request) { - json.NewEncoder(w).Encode(session.Getses()) - }) - handler.HandleFunc("/signin", func(w http.ResponseWriter, r *http.Request) { switch r.Method { case http.MethodGet: diff --git a/session/session.go b/session/session.go index 0bef255..47a62fe 100644 --- a/session/session.go +++ b/session/session.go @@ -13,7 +13,7 @@ type Session struct { Values map[string]interface{} } -type StoreSession struct { +type SessionStore struct { Sessions map[string]*Session mu sync.Mutex } @@ -29,27 +29,27 @@ type SessionInfo struct { AccessAt string } -type ListSessionInfo map[string][]*SessionInfo +type SessionInfoList map[string][]*SessionInfo -var Store = StoreSession{Sessions: make(map[string]*Session)} -var UserSessions = make(ListSessionInfo) +var GlobalSessionStore = SessionStore{Sessions: make(map[string]*Session)} +var UserSessionInfoList = make(SessionInfoList) -type SessionNotFound struct{} +type SessionNotFoundError struct{} -func (e *SessionNotFound) Error() string { +func (e *SessionNotFoundError) Error() string { return "session not found" } -func (s *StoreSession) Get(id string) (*Session, error) { +func (s *SessionStore) Get(id string) (*Session, error) { s.mu.Lock() defer s.mu.Unlock() if session, ok := s.Sessions[id]; ok { return session, nil } - return nil, &SessionNotFound{} + return nil, &SessionNotFoundError{} } -func (s *StoreSession) Create() *Session { +func (s *SessionStore) Create() *Session { id := utils.GenerateRandomString(128) session := &Session{ ID: id, @@ -59,7 +59,7 @@ func (s *StoreSession) Create() *Session { return session } -func (s *StoreSession) Delete(id string) { +func (s *SessionStore) Delete(id string) { s.mu.Lock() defer s.mu.Unlock() delete(s.Sessions, id) @@ -82,48 +82,44 @@ func (s *Session) Destroy(w http.ResponseWriter) { }) } -func AppendSession(email string, sessionInfo *SessionInfo) { - UserSessions[email] = append(UserSessions[email], sessionInfo) +func AddSessionInfo(email string, sessionInfo *SessionInfo) { + UserSessionInfoList[email] = append(UserSessionInfoList[email], sessionInfo) } -func RemoveSession(email string, id string) { - sessions := UserSessions[email] - var updatedSessions []*SessionInfo - for _, userSession := range sessions { - if userSession.SessionID != id { - updatedSessions = append(updatedSessions, userSession) +func RemoveSessionInfo(email string, id string) { + sessionInfos := UserSessionInfoList[email] + var updatedSessionInfos []*SessionInfo + for _, sessionInfo := range sessionInfos { + if sessionInfo.SessionID != id { + updatedSessionInfos = append(updatedSessionInfos, sessionInfo) } } - if len(updatedSessions) > 0 { - UserSessions[email] = updatedSessions + if len(updatedSessionInfos) > 0 { + UserSessionInfoList[email] = updatedSessionInfos return } - delete(UserSessions, email) + delete(UserSessionInfoList, email) } -func RemoveAllSession(email string) { - sessions := UserSessions[email] - for _, session := range sessions { - delete(Store.Sessions, session.SessionID) +func RemoveAllSessions(email string) { + sessionInfos := UserSessionInfoList[email] + for _, sessionInfo := range sessionInfos { + delete(GlobalSessionStore.Sessions, sessionInfo.SessionID) } - delete(UserSessions, email) + delete(UserSessionInfoList, email) } func GetSessionInfo(email string, id string) *SessionInfo { - for _, session := range UserSessions[email] { - if session.SessionID == id { - return session + for _, sessionInfo := range UserSessionInfoList[email] { + if sessionInfo.SessionID == id { + return sessionInfo } } return nil } -func (user *SessionInfo) UpdateAccessTime() { +func (sessionInfo *SessionInfo) UpdateAccessTime() { currentTime := time.Now() formattedTime := currentTime.Format("01-02-2006") - user.AccessAt = formattedTime -} - -func Getses() *ListSessionInfo { - return &UserSessions + sessionInfo.AccessAt = formattedTime }