Refactor session code

This commit is contained in:
2024-05-05 14:50:51 +07:00
parent 19856c328d
commit dce11e3569
5 changed files with 18 additions and 35 deletions

View File

@ -167,7 +167,7 @@ func GET(w http.ResponseWriter, r *http.Request) {
log.Error(err.Error())
return
}
storeSession := session.GlobalSessionStore.Create()
storeSession := session.Create()
storeSession.Values["user"] = types.User{
UserID: user.UserID,
Email: oauthUser.Email,

View File

@ -130,7 +130,7 @@ func POST(w http.ResponseWriter, r *http.Request) {
delete(SetupUser, code)
storeSession := session.GlobalSessionStore.Create()
storeSession := session.Create()
storeSession.Values["user"] = types.User{
UserID: userID,
Email: unregisteredUser.Email,

View File

@ -4,25 +4,18 @@ import (
"errors"
"net/http"
"github.com/fossyy/filekeeper/logger"
"github.com/fossyy/filekeeper/session"
"github.com/fossyy/filekeeper/types"
"github.com/fossyy/filekeeper/utils"
)
var log *logger.AggregatedLogger
func init() {
log = logger.Logger()
}
func GET(w http.ResponseWriter, r *http.Request) {
cookie, err := r.Cookie("Session")
if err != nil {
return
}
storeSession, err := session.GlobalSessionStore.Get(cookie.Value)
storeSession, err := session.Get(cookie.Value)
if err != nil {
if errors.Is(err, &session.SessionNotFoundError{}) {
storeSession.Destroy(w)
@ -31,7 +24,7 @@ func GET(w http.ResponseWriter, r *http.Request) {
return
}
session.GlobalSessionStore.Delete(cookie.Value)
storeSession.Delete()
session.RemoveSessionInfo(storeSession.Values["user"].(types.User).Email, cookie.Value)
http.SetCookie(w, &http.Cookie{

View File

@ -58,7 +58,7 @@ func POST(w http.ResponseWriter, r *http.Request) {
}
if email == userData.Email && utils.CheckPasswordHash(password, userData.Password) {
storeSession := session.GlobalSessionStore.Create()
storeSession := session.Create()
storeSession.Values["user"] = types.User{
UserID: userData.UserID,
Email: email,